PAAS平台的web应用性能测试与分析
缩块找奇环.....
Knights of the Round Table
Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced
an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere,
while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines
contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).
The input is terminated by a block with n = m = 0 . Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.
Sample Input 5 5 1 4 1 5 2 5 3 4 4 5 0 0 Sample Output 2 Hint
Huge input file, ‘scanf‘ recommended to avoid TLE.
Source |
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1100; struct Edge { int to,next; }edge[maxn*21]; int Adj[maxn],Size=0; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } bool A[maxn][maxn]; int n,m; int DFN[maxn],Instack[maxn],Low[maxn],Belong[maxn],Color[maxn],ex[maxn]; int Stack[maxn],tempStack[maxn]; int Index,top,temptop,scc; bool is_odd_cycle(int u,int fa,int SCC,int cor) { Color[u]=cor; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(Belong[v]!=SCC||v==fa) continue; if(Color[v]==-1) { if(is_odd_cycle(v,u,SCC,1-cor)==true) return true; } else { if(Color[v]==Color[u]) return true; } } return false; } void Tarjan(int u,int fa) { DFN[u]=Low[u]=++Index; Instack[u]=true; Stack[top++]=u; int v; for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(!DFN[v]) { Tarjan(v,u); Low[u]=min(Low[u],Low[v]); if(Low[v]>=DFN[u]) { temptop=0; scc++; int tempoint=-999; do { tempoint=Stack[--top]; Belong[tempoint]=scc; Instack[tempoint]=false; tempStack[temptop++]=tempoint; }while(tempoint!=v); tempStack[temptop++]=u; memset(Color,-1,sizeof(Color)); if(temptop>=3&&is_odd_cycle(u,u,scc,1)) { for(int j=0;j<temptop;j++) { ex[tempStack[j]]=1; } } } } else { Low[u]=min(Low[u],DFN[v]); } } } void scc_solve() { memset(DFN,0,sizeof(DFN)); memset(Instack,0,sizeof(Instack)); memset(Low,0,sizeof(Low)); memset(Belong,0,sizeof(Belong)); Index=top=scc=temptop=0; for(int i=1;i<=n;i++) if(!DFN[i]) Tarjan(i,i); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(A,0,sizeof(A)); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); A[a][b]=A[b][a]=true; } init(); for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(A[i][j]==true) continue; Add_Edge(i,j); Add_Edge(j,i); } } memset(ex,0,sizeof(ex)); scc_solve(); int ans=n; for(int i=1;i<=n;i++) { if(ex[i]) ans--; } printf("%d\n",ans); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。