wyh2000 and pupil
wyh2000 and pupil
Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) 問題描寫敘述青年理論計算機科學家wyh2000在教導他的小學生。 共同擁有個小學生,編號為。為了添加小學生之間的凝聚力,wyh2000決定將全部小學生分成組,每組都至少有個人。 可是有些小學生之間並不認識,並且假設不認識,那麽也不認識。輸入描寫敘述Wyh2000希望每組中的小學生都互相認識。並且第一組的人要盡可能多。 請你幫wyh2000求出第一組和第二組的人數是多少。假設找不到分組方案,則輸出"Poor wyh"。
第一行一個數,表示數據組數。 對於每組數據,第一行兩個數,表示小學生數量和互相不認識的小學生的數量。輸出描寫敘述接下來行,每行兩個數,表示不認識,不認識。
保證一對僅僅會出現一次。
對於每組數據,輸出答案。輸入例子
2 8 5 3 4 5 6 1 2 5 8 3 5 5 4 2 3 4 5 3 4 2 4輸出例子
5 3 Poor wyh
Statistic | Submit | Clarifications | Back/* Author: 2486 Memory: 7448 KB Time: 592 MS Language: G++ Result: Accepted VJ RunId: 4055769 Real RunId: 14058285 Public: No Yes */ /* 假設a不認識b,那麽在a,b間連一條邊,這樣有解當且僅當這張圖是二分圖。 由於可能有多個二分圖。而題目要求第一組的人盡可能多,所以貪心的選擇就可以。要註意m=0的情況。
*/ #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=100000+5; vector<int>G[maxn]; int col[maxn],T; int n,m,a,b,acnt,bcnt; void init(int x) { for(int i=1; i<=x; i++) { G[i].clear(); } } bool bfs(int u) { queue<int>k; k.push(u); col[u]=1; while(!k.empty()) { int s=k.front(); if(col[s]==1)acnt++; else bcnt++; k.pop(); for(int i=0; i<G[s].size(); i++) { if(col[G[s][i]]==-1) { col[G[s][i]]=!col[s]; k.push(G[s][i]); continue; } if(col[G[s][i]]==col[s])return false; } } return true; } void slove() { memset(col,-1,sizeof(col)); bool flag=false; int Max=0; for(int i=1; i<=n; i++) { acnt=0,bcnt=0; if(col[i]==-1&&!bfs(i)) { flag=true; break; } Max+=max(acnt,bcnt);//必須這麽做,由於這裏面為1的或者為0的不一定就是同一陣營。
} if(flag)printf("Poor wyh\n"); else printf("%d %d\n",Max,n-Max); } int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); init(n); for(int i=0; i<m; i++) { scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } if(n<2) {//題目要求 printf("Poor wyh\n"); continue; } if(m==0) {//題目要求 printf("%d 1\n",n-1); continue; } slove(); } return 0; }
wyh2000 and pupil