T1:The Largest Clique
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<set> 5 #define MN 1005 6 #define ME 50005 7 #define mst(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 inline int in(){ 10 int x=0;bool f=0; char c; 11 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 12 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 13 return f?-x:x; 14 } 15 struct edge{ 16 int to,next; 17 }e[ME],r[ME]; 18 set <int> ed[MN]; 19 int head[MN],rhed[MN],low[MN],dfn[MN],scc[MN],st[MN],siz[MN],f[MN]; 20 bool vis[MN]; 21 int cnt=0,rct=0,top,dfsn,ct,ans,n,m,T,u,w; 22 inline int min(int a,int b){return a<b?a:b;} 23 inline int max(int a,int b){return a>b?a:b;} 24 inline void ins(int x,int y){ 25 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt; 26 } 27 inline void rins(int x,int y){ 28 r[++rct].to=y;r[rct].next=rhed[x];rhed[x]=rct; 29 } 30 inline void tarjan(int x){ 31 low[x]=dfn[x]=(++dfsn);vis[x]=1;st[++top]=x; 32 for (int i=head[x];i;i=e[i].next){ 33 int v=e[i].to; 34 if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]); 35 else if (vis[v]) low[x]=min(low[x],dfn[v]); 36 }if (dfn[x]==low[x]){ 37 int cur=0;siz[++ct]=0; 38 while (cur!=x){ 39 cur=st[top--];vis[cur]=0; 40 ++siz[ct];scc[cur]=ct; 41 } 42 } 43 } 44 inline int dp(int x){ 45 if (f[x]!=-1) return f[x];int pre=0; 46 for (int j=rhed[x];j;j=r[j].next){ 47 int v=r[j].to;pre=max(pre,dp(v)); 48 }f[x]=pre+siz[x];return f[x]; 49 50 } 51 inline void init(){ 52 mst(e,0);mst(r,0);mst(siz,0);mst(scc,0);mst(st,0); 53 mst(dfn,0);mst(low,0);mst(head,0);mst(rhed,0);mst(vis,0); 54 } 55 int main() 56 { 57 T=in();while (T--){ 58 n=in();m=in();dfsn=top=ct=0;cnt=rct=0;init(); 59 for (int i=1;i<=m;++i) u=in(),w=in(),ins(u,w); 60 for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i); 61 for (int i=1;i<=ct;++i) ed[i].clear(); 62 for (int i=1;i<=n;++i) 63 for (int j=head[i];j;j=e[j].next){ 64 int v=e[j].to,su=scc[i],sv=scc[v]; 65 if (su==sv||ed[su].count(sv)) continue; 66 ed[su].insert(sv);rins(sv,su); 67 }mst(f,-1);ans=0; 68 for (int i=1;i<=ct;++i) ans=max(ans,dp(i));printf("%d ",ans); 69 }return 0; 70 }
T2:Network of Schools
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<set> 6 #define MN 102 7 using namespace std; 8 inline int rd(){ 9 int x=0;bool f=0; char c; 10 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 11 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 12 return f?-x:x; 13 } 14 struct edge{ 15 int to,next; 16 }e[MN*MN]; 17 set <int> ed[MN]; 18 int head[MN],low[MN],dfn[MN],scc[MN],st[MN],in[MN],out[MN]; 19 int cnt=0,top=0,dfsn=0,ct=0,rt,lv,n,b; 20 bool vis[MN]; 21 22 inline int min(int a,int b){return a<b?a:b;} 23 inline int max(int a,int b){return a>b?a:b;} 24 inline void ins(int x,int y){ 25 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt; 26 } 27 inline void tarjan(int x){ 28 low[x]=dfn[x]=(++dfsn);vis[x]=1;st[++top]=x; 29 for (int i=head[x];i;i=e[i].next){ 30 int v=e[i].to; 31 if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]); 32 else if (vis[v]) low[x]=min(low[x],dfn[v]); 33 }if (dfn[x]==low[x]){ 34 int cur=0;++ct;//siz[++ct]=0; 35 while (cur!=x){ 36 cur=st[top--];vis[cur]=0; 37 scc[cur]=ct;//++siz[ct]; 38 } 39 } 40 } 41 int main() 42 { 43 n=rd(); 44 for (int a=1;a<=n;++a) {b=rd();while (b) ins(a,b),b=rd();} 45 for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i); 46 if (ct==1) {printf("1 0 ");return 0;} 47 for (int i=1;i<=ct;++i) ed[i].clear(); 48 for (int i=1;i<=n;++i) 49 for (int j=head[i];j;j=e[j].next){ 50 int v=e[j].to,su=scc[i],sv=scc[v]; 51 if (su==sv||ed[su].count(sv)) continue; 52 ed[su].insert(sv);++in[sv];++out[su]; 53 }for (int i=1;i<=ct;++i) {if (!in[i]) ++rt;if (!out[i])++lv;} 54 printf("%d %d ",rt,max(rt,lv)); 55 return 0; 56 }
T3:Proving Equivalences
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define ME 50005 6 #define MN 20005 7 #define mst(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 inline int rd(){ 10 int x=0,f=1; char ch=getchar(); 11 while(ch<'0'||ch>'9') f=ch=='-'?-1:1,ch=getchar(); 12 while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); 13 return x*f; 14 } 15 struct edge{ 16 int to,next; 17 }e[ME]; 18 int head[MN],low[MN],dfn[MN],scc[MN],st[MN],siz[MN],in[MN],out[MN]; 19 int cnt=0,top=0,dfsn=0,ct=0,rt,lv,n,m,s1,s2,T; 20 bool vis[MN]; 21 inline int min(int a,int b){return a<b?a:b;} 22 inline int max(int a,int b){return a>b?a:b;} 23 inline void ins(int x,int y){ 24 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt; 25 } 26 inline void init(){ 27 mst(e,0);mst(siz,0);mst(st,0); 28 mst(scc,0);mst(dfn,0);mst(low,0); 29 mst(head,0);mst(vis,0);cnt=rt=lv=0; 30 mst(in,0);mst(out,0);dfsn=top=ct=0; 31 } 32 inline void tarjan(int x){ 33 low[x]=dfn[x]=(++dfsn);vis[x]=1;st[++top]=x; 34 for (int i=head[x];i;i=e[i].next){ 35 int v=e[i].to; 36 if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]); 37 else if (vis[v]) low[x]=min(low[x],dfn[v]); 38 }if (low[x]==dfn[x]){ 39 int cur=0;siz[++ct]=0; 40 while (cur!=x){ 41 cur=st[top--];vis[cur]=0; 42 scc[cur]=ct;++siz[ct]; 43 } 44 } 45 } 46 int main () 47 { 48 T=rd();while (T--){ 49 n=rd();m=rd();init(); 50 for (int i=1;i<=m;++i) s1=rd(),s2=rd(),ins(s1,s2); 51 for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i); 52 if (ct==1){printf("0 ");continue;} 53 for (int i=1;i<=n;++i) 54 for (int j=head[i];j;j=e[j].next){ 55 int v=e[j].to,cx=scc[i],cy=scc[v]; 56 if (cx==cy) continue;++in[cy];++out[cx]; 57 }for (int i=1;i<=ct;++i){ 58 if (!in[i]) ++rt;if (!out[i])++lv; 59 }printf("%d ",max(rt,lv)); 60 } 61 return 0; 62 }
T4:Popular Cows
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ME 50005 5 #define MN 10005 6 using namespace std; 7 inline int in(){ 8 int x=0;bool f=0; char c; 9 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 10 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 11 return f?-x:x; 12 } 13 struct edge{ 14 int to,next; 15 }e[ME]; 16 int head[MN],dfn[MN],low[MN],st[MN],siz[MN],scc[MN],out[MN]; 17 int top,dfsn=0,n,m,a,b,cnt=0,ct,pos; 18 bool vis[MN],lv; 19 inline void ins(int x,int y){ 20 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt; 21 } 22 void tarjan (int x){ 23 low[x]=dfn[x]=(++dfsn);vis[x]=1;st[++top]=x; 24 for (int i=head[x];i;i=e[i].next){ 25 int v=e[i].to; 26 if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]); 27 else if (vis[v]) low[x]=min(low[x],dfn[v]); 28 }if (low[x]==dfn[x]){ 29 int cur=0;siz[++ct]=0; 30 while (cur!=x){ 31 cur=st[top--];vis[cur]=0; 32 scc[cur]=ct;++siz[ct]; 33 } 34 } 35 } 36 int main () 37 { 38 n=in();m=in();top=ct=lv=pos=0; 39 for (int i=1;i<=m;++i) a=in(),b=in(),ins(a,b); 40 for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i); 41 for (int i=1;i<=n;++i) 42 for (int j=head[i];j;j=e[j].next) 43 if (scc[i]!=scc[e[j].to]) ++out[scc[i]]; 44 for (int i=1;i<=ct;++i) if (!out[i]) 45 if (lv) {printf("0");return 0;}else lv=1,pos=i; 46 printf("%d",siz[pos]);return 0; 47 }