您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: