HDU 5961 传递(暴力枚举)
2016-11-11 19:21
302 查看
题目分析
直接判断判断P和Q是否为传递图即可。#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2020; const int maxm = maxn*maxn; struct Edge{ int to, next; }e1[maxm], e2[maxm]; int head1[maxn], head2[maxn], tot1, tot2; char maze[maxn][maxn]; void addedge1(int from, int to){ e1[tot1].to = to; e1[tot1].next = head1[from]; head1[from] = tot1++; } void addedge2(int from, int to){ e2[tot2].to = to; e2[tot2].next = head2[from]; head2[from] = tot2++; } bool dfs1(int u){ for(int i = head1[u]; i != -1; i = e1[i].next){ int x = e1[i].to; for(int j = head1[x]; j != -1; j = e1[j].next){ int v = e1[j].to; if(maze[u][v] != 'P') return false; } } return true; } bool dfs2(int u){ for(int i = head2[u]; i != -1; i = e2[i].next){ int x = e2[i].to; for(int j = head2[x]; j != -1; j = e2[j].next){ int v = e2[j].to; if(maze[u][v] != 'Q') return false; } } return true; } void init(){ tot1 = tot2 = 0; memset(head1, -1, sizeof(head1)); memset(head2, -1, sizeof(head2)); } int main(){ int T, n; scanf("%d", &T); while(T--){ init(); scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%s", maze[i]); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(maze[i][j] == 'P') addedge1(i, j); else if(maze[i][j] == 'Q') addedge2(i, j); int flag = true; for(int i = 0; i < n; i++) if(!dfs1(i)){ flag = false; break; } if(!flag){ printf("N\n"); continue; } for(int i = 0; i < n; i++) if(!dfs2(i)){ flag = false; break; } if(flag) printf("T\n"); else printf("N\n"); } return 0; }
相关文章推荐
- Hdu 5961 传递【暴力枚举】
- HDU-5961 传递(暴力)
- HDU 4151 The Special Number【二分+暴力枚举】
- hdu 4007 暴力枚举 Dave
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
- hdu 4414 暴力枚举
- HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
- HDU1238_搜索入门_暴力枚举
- hdu 1172 猜数字(暴力枚举)
- HDU 4082 Hou Yi's secret 暴力枚举
- hdu1172(暴力枚举)
- HDU 1172猜数字(直接暴力枚举即可)
- hdu 1172 猜数字 解题报告<暴力枚举>
- hdu 1557暴力枚举
- hdu 1077 Catching Fish 计算几何+暴力枚举
- HDU 4331Image Recognition2012多校第四场A题(暴力枚举+小技巧)
- hdu 3332 暴力枚举
- hdu 3325 Arithmetically Challenged(dfs全排+暴力枚举破解+set集合的应用)
- hdu - 4608 - I-number(暴力枚举)
- hdu——4462(暴力枚举)