您的位置:首页 > 其它

Light 1009 - Back to Underworld (dfs)

2015-05-15 22:31 357 查看

题意

两个国家打仗,现在只知道战场,想问最多的战场数量。

思路

相当于对一个图进行黑白染色,我们取颜色多的那部分加起来即可。

因为题目没说不符合条件的怎么办,所以可以认为数据全部合法。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 2e5+10;
const int MOD = 10000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;

vector<int> G[MAXN];
int vis[MAXN], pass;

int dfs(int cur, int color)
{
vis[cur] = color;
pass++;
int ret = 0;
FOR(i, 0, SZ(G[cur]))
{
int u = G[cur][i];
if (!vis[u]) ret += dfs(u, 3-color);
}
return color == 1 ? ret+1 : ret;
}

int main()
{
//ROP;
int T;
scanf("%d", &T);
while (T--)
{
MS(vis, 0);
int n;
scanf("%d", &n);
FOR(i, 1, MAXN) G[i].clear();
while (n--)
{
int a, b;
scanf("%d%d", &a, &b);
G[a].PB(b); G[b].PB(a);
}
int ans = 0;
FOR(i, 1, MAXN)
{
pass = 0;
int number;
if (!vis[i] && !G[i].empty())
{
number = dfs(i, 1);
ans += max(number, pass-number);
}
}
printf("Case %d: %d\n", ++cases, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm