HDU 5285 wyh2000 and pupil(dfs或种类并查集)

wyh2000 and pupil

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 755    Accepted Submission(s): 251

Problem Description

Young theoretical computer scientist wyh2000 is teaching his pupils.

Wyh2000 has n pupils.Id of them are from
to .In
order to increase the cohesion between pupils,wyh2000 decide to divide them into 2 groups.Each group has at least 1 pupil.

Now that some pupils don‘t know each other(if
doesn‘t know ,then

doesn‘t know ).Wyh2000
hopes that if two pupils are in the same group,then they know each other,and the pupils of the first group must be as much as possible.

Please help wyh2000 determine the pupils of first group and second group. If there is no solution, print "Poor wyh".

Input

In the first line, there is an integer

indicates the number of test cases.

For each case, the first line contains two integers

indicate the number of pupil and the number of pupils don‘t konw each other.

In the next m lines,each line contains 2 intergers
<,indicates
that
don‘t know
and
don‘t know ,the
pair
will only appear once.

Output

For each case, output the answer.

Sample Input

2
8 5
3 4
5 6
1 2
5 8
3 5
5 4
2 3
4 5
3 4
2 4

Sample Output

5 3
Poor wyh

大致题意:

n个点分成两组,m条边,每条边连接的两个点必须是在不同的两组,且第一组要尽量的大

思路:显然。dfs染色,不是二分图就无解。

还能够用种类并查集来做,把全部可能性合并,由对称性可知,仅仅统计1~n是祖先时。看祖先的子节点中黑白节点是多少个就ok

//468MS 4036K 1981 B C++
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii;

const int N = 1e5+100;

int sz[N*2];
int sz2[N*2];
int fa[N*2];
int n,m;
void ini(){
        REP(i,2*n) fa[i] = i;
        REP(i,2*n) sz[i] = (i <= n);
        REP(i,2*n) sz2[i] = (i > n);
}
int getf(int x){
        return x == fa[x] ? x : fa[x] = getf(fa[x]);
}
bool same(int a,int b){
        return getf(a) == getf(b);
}
void Merge(int a,int b){
        int f1 = getf(a), f2 = getf(b);
        if(f1 == f2) return ;
        fa[f1] = f2;
        sz[f2] += sz[f1];
        sz2[f2] += sz2[f1];
}
int main(){
        int T;
        cin>>T;
        while(T--){
                scanf("%d%d",&n,&m);
                ini();
                bool flag = 0;
                REP(i,m){
                        int a,b;
                        scanf("%d%d",&a,&b);
                        if(flag) continue;
                        if(same(a,b) || same(a+n,b+n) ) flag = 1;
                        else Merge(a+n,b),Merge(a,b+n);
                }
                if( n < 2 || flag) puts("Poor wyh");
                else if(m == 0) printf("%d 1\n",n-1);
                else {
                        int ans = 0;
                        REP(i,n){
                                if( fa[i] == i) ans += min(sz[i],sz2[i]);
                        }
                        printf("%d %d\n",n-ans,ans);
                }

        }
}

时间: 2024-10-30 21:47:28

HDU 5285 wyh2000 and pupil(dfs或种类并查集)的相关文章

HDU 5285 wyh2000 and pupil (DFS染色判二分图 + 贪心)

wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1040    Accepted Submission(s): 331 Problem Description Young theoretical computer scientist wyh2000 is teaching his pupils. W

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri

HDU 1829 A Bug&#39;s Life (种类并查集)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19429    Accepted Submission(s): 6206 Problem Description Background Professor H

hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges

HDU 5285 wyh2000 and pupil (二分图)

题意:共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组的人要尽可能多.请你帮wyh2000求出第一组和第二组的人数是多少.如果找不到分组方案,则输出"Poor wyh". 思路:二分图着色.给的就是无向图,每次都累加人多的颜色即可.若不能着色,必定不能分成2组.如果全部都是1个颜色,那么要让其中1人过第2组.我勒个去,就因为赭色时颜色号码开小了

hdu 5258 wyh2000 and pupil(dfs)(待续)

题意:n个点,m条边,每条边连接的两点颜色不同, 思路: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int t,n,m; vector<int>g[500010]; int vis[500010]; int cnt1,cnt2,sum; int dfs(int

Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比二班的人数多,每个班的人数都大于零. 解题思路: hdu给出的题解是二分图匹配加上贪心,就不多说了. 还可以用bfs对节点染色,建好图后,对节点进行bfs分成,偶数成与奇数成染成不同的颜色,颜色相同的节点都可以分到同一个集合里面,但是要判断一下奇环,如果出现奇环的话,是无法进行分组的.在每次bfs的

hdu 5285 wyh2000 and pupil(二分图判定)

对每两个不认识的人连一条边,则此题可转化为二分图判定(二分图可有多个). 如果有一部分图判定为不是二分图,则输出“Poor wyh”. 否则,分别累加每个二分图的最多的颜色数. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmat

HDU 5285 wyh2000 and pupil

题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的……但是为了挣扎一下百度了一下二分图的判定方法,知道了可以用染色法,这样如果是二分图的话将每个连通分量里点数量最多的颜色的点数量(像个绕口令诶)相加就可以了.然而激动万分的我早忘了还有每部分至少一人这个条件……直到我和队友研究怎么hack别人的时候他才告诉我还有这么个条件……(哭)还好来得及…… 代