Back to Underworld
Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1009
Description
The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to the battlefield.
So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don‘t know which one of them is a Vampire or a Lykan.
So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between u and v. No rival will be reported more than once.
Output
For each case, print the case number and the maximum possible members of any race.
Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
Sample Output
Case 1: 2
Case 2: 3
题解:两拨人打架,问最每队最大的可能人数是多少;搜索下。。。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<map> #include<vector> using namespace std; const int INF = 0x3f3f3f3f; #define mem(x, y) memset(x, y, sizeof(x)) typedef long long LL; void SI(int &x){scanf("%d", &x);} void SI(LL &x){scanf("%lld", &x);} const int MAXN = 1e5 + 100; int vis[MAXN], usd[MAXN]; int sum1, sum2; vector<int>vec[MAXN]; void dfs(int i, int cur){ if(cur)sum1++; else sum2++; // vis[i] = 1; for(int j = 0; j < vec[i].size(); j++){ int v = vec[i][j]; if(vis[v])continue; vis[v] = 1; dfs(v, cur ^ 1); } } int main(){ int T,kase = 0, n; scanf("%d", &T); while(T--){ scanf("%d", &n); int x, y, tp = 0; for(int i = 0; i <= MAXN; i++){ vis[i] = 0; usd[i] = 0; vec[i].clear(); } for(int i = 0; i < n; i++){ scanf("%d%d", &x, &y); vec[x].push_back(y); vec[y].push_back(x); usd[x] = usd[y] = 1; tp = max(tp, x); tp = max(tp, y); } int ans = 0; //printf("tp = %d\n", tp); for(int i = 1; i <= tp; i++){ if(vis[i] || !usd[i])continue; sum1 = sum2 = 0; vis[i] = 1; dfs(i, 0); // printf("%d %d %d \n", sum1,sum2,i); ans += max(sum1, sum2); } printf("Case %d: %d\n", ++kase, ans); } return 0; }