NewTrain1 T2: [ZJOI2007] Game matrix

Topic analysis

Obviously, if the row as a '1' in the j-th, then this line can be switched to the j-th row. Another words, the subject is to allow us to determine whether there is a program that has a column on each row i i meet position is 1 line to match him.

Obviously this is a bipartite graph matching (Column -> Line), built directly map to run Dinic.

  1 #include<bits/stdc++.h>
  2 #define INTMAX 2147483647LL
  3 #define PII pair<int,int>
  4 #define MK make_pair
  5 #define re register
  6 using namespace std;
  7 typedef long long ll;
  8 const double Pi=acos(-1.0);
  9 const int Inf=0x3f3f3f3f;
 10 const int MAXN=405;
 11 inline int read(){
 12     re int x=0,f=1,ch=getchar();
 13     while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
 14     while(isdigit(ch))x=x*10+ch-48,ch=getchar();
 15     return x*f;
 16 }
 17 inline ll readll(){
 18     re ll x=0,f=1,ch=getchar();
 19     while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
 20     while(isdigit(ch))x=x*10+ch-48,ch=getchar();
 21     return x*f;
 22 }
 23 
 24 struct Edge{
 25     int to,nxt,cap;
 26 }e[MAXN*MAXN<<1];
 27 int cnt=1,head[MAXN];
 28 inline void add_edge(int u,int v,int cap){
 29     e[++cnt].to=v;e[cnt].cap=cap;e[cnt].nxt=head[u];head[u]=cnt;
 30     e[++cnt].to=u;e[cnt].cap=0;e[cnt].nxt=head[v];head[v]=cnt;
 31 } 
 32 
 33 int TT,n,S,T,Maxflow;
 34 int dep[MAXN];
 35 inline void Init(){
 36     cnt=1;Maxflow=0;
 37     memset(head,0,sizeof(head));
 38     memset(e,0,sizeof(e));
 39 }
 40 queue<int> q;
 41 inline bool bfs(){
 42     while(!q.empty()) q.pop();
 43     memset(dep,0,sizeof(dep));
 44     dep[S]=1;q.push(S);
 45     while(!q.empty()){
 46         int x=q.front();q.pop();
 47         for(int i=head[x],y;i;i=e[i].nxt){
 48             y=e[i].to;
 49             if(e[i].cap&&!dep[y]){
 50                 dep[y]=dep[x]+1;
 51                 q.push(y);
 52             }
 53         }
 54     }
 55     return dep[T];
 56 } 
 57 inline int dfs(int x,int flow){
 58     if(x==T||!flow)    return flow;
 59     int used=0;
 60     for(int i=head[x],y;i;i=e[i].nxt){
 61         y=e[i].to;
 62         if(e[i].cap&&dep[y]==dep[x]+1){
 63             int f=dfs(y,min(e[i].cap,flow-used));
 64             if(f){
 65                 e[i].cap-=f;
 66                 e[i^1].cap+=f;
 67                 used+=f;
 68                 if(used==flow) break;
 69             }
 70         }
 71     }
 72     return used;
 73 }
 74 
 75 inline void Dinic(){
 76     while(bfs()){
 77         Maxflow+=dfs(S,Inf);
 78     }
 79 }
 80 int main(){
 81     TT=read();
 82     while(TT--){
 83         Init();
 84         n=read();
 85         S=402;T=403;
 86         for(int i=1,x;i<=n;++i){
 87             add_edge(S,i,1);
 88             add_edge(i+n,T,1);
 89             for(int j=1;j<=n;++j){
 90                 x=read();
 91                 if(x)
 92                     add_edge(i,n+j,1);
 93             }
 94         }
 95         Dinic();
 96         if(Maxflow==n) puts("Yes");
 97         else puts("No");
 98     }
 99     return 0;
100 }

 

Guess you like

Origin www.cnblogs.com/LI-dox/p/11257633.html