题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1059
分析:
我们可以轻而易举发现。无论怎么在图中交换行。交换列。它属于哪个行属于哪个列这个是不会变的。
于是问题就可以转化为 能否找到n个互不同行或者同列的点.
Tags:逆向思维.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include<bits/stdc++.h> using namespace std; template<class T>void read(T &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); } while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } x = f ? -x : x; return ; } struct node{ int v,next; }edge[50100]; int head[300]; int match[300]; bool visit[300]; int n,cnt; void init() { cnt=0; memset(head,0xff,sizeof(head)); memset(match,0xff,sizeof(match)); return ; } void add_edge(int x,int y) { edge[++cnt].v=y; edge[cnt].next=head[x]; head[x]=cnt; return ; } bool Find_edge(int x) { for(int i=head[x];~i;i=edge[i].next) { if(visit[edge[i].v])continue; visit[edge[i].v]=true; if(match[edge[i].v]==-1||Find_edge(match[edge[i].v])) { match[edge[i].v]=x; return true; } } return false; } bool Huarry() { for(int i=1;i<=n;++i) { memset(visit,false,sizeof(visit)); if(!Find_edge(i))return false; } return true; } int main() { //freopen("./in","r",stdin); int t,k; read(t); while(t--) { init(); read(n); for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { read(k); if(k)add_edge(i,j); } } if(Huarry())printf("Yes\n"); else printf("No\n"); } // fclose(stdin); return 0; } |