1918: G
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 332 Solved: 70
Description
晴天也来寻宝啦,有一个m层的宝塔,只能从第一层开始一层一层的往上走,每层都有一个门,你需要用钥匙来打开门才能继续走,现在晴天有n把钥匙,编号为0-n-1,然后他要开始寻宝了。没有特殊技能怎么好意思出来寻宝呢,他现在有两个天赋技能,他知道第i层的门可以用编号为a和b的钥匙打开(可能a等于b呦),然后他还可以在进入宝塔前把门的顺序任意调换一次,也就是说比如可以把m层原来的1 2 3 ..m,换为 m ...3 2 1.晴天想知道他最多能拿到多少层的宝物。
Input
第一行一个整数t表示有多少组测试实例
每组数据第一行为两个整数n,m分别表示有多少个钥匙,有多少层。
接下来m行,每行两个数字x,y,第i行表示第i层的门可以用标号x或y的钥匙打开。
(n,m<=1000)
Output
输出一个整数表示最多可以上多少层。
Sample Input
13 40 10 10 11 2
Sample Output
3
HINT
在样例中,在进入宝塔前,将门的顺序换为4 1 2 3.然后前三层分别使用2 0 1三把钥匙拿到前三层的宝物
#include <cmath> #include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #define space " " using namespace std; //typedef __int64 Int; //typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 1000 + 10; const double Pi = acos(-1.0); const double ESP = 1e-5; vector<int> G[MAXN]; int match[MAXN]; bool used[MAXN]; void init() { for (int i = 0; i < MAXN - 1; i++) { G[i].clear(); match[i] = -1; } } bool Dfs(int x) { for (int i = 0; i < G[x].size(); i++) { int t = G[x][i]; if (used[t]) continue; used[t] = true; if (match[t] == -1 || Dfs(match[t])) { match[t] = x; return true; } } return false; } int main() { int n, m, a, b, t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); init(); for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); if (a == b && a < n && a >= 0) G[i].push_back(a); else { if (a < n && a >= 0) G[i].push_back(a); if (b < n && b >= 0) G[i].push_back(b); } } int ans = 0; for (int i = 1; i <= m; i++) { memset(used, false, sizeof(used)); if (Dfs(i)) ans++; } printf("%d ", ans); } return 0; }