#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <limits.h>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 3e5+10, mod = 1e9 + 9;
const long double eps = 1e-5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{
read(first);
read(args...);
}
int T;
struct node {
int to, next;
}e[N];
int head[N], idx;
map<int,int> mp;
int count_edge, count_var;
inline void add(int from, int to)
{
e[idx] = {to,head[from]};
head[from] = idx ++;
}
inline void dfs(int u)
{
if(u) mp[u] = 1;
for(int &i = head[u]; ~i;)
{
int v = e[i].to;//复习了一波删边操作
i = e[i].next;
count_edge ++;
dfs(v);
}
}
void init()
{
mp.clear();
idx = 0;
ms(head,-1);
}
int main()
{
read(T);
int Case = 1;
while(T --)
{
int n;
int cnt = 0;
read(n);
init();
for(int i = 0; i < n; ++ i)
{
int l, r;
read(l,r);
if(!mp.count(l)) mp[l] = ++ cnt;
if(!mp.count(r)) mp[r] = ++ cnt;
add(mp[l],mp[r]);
add(mp[r],mp[l]);
}
mp.clear();
int ans = 0;
for(int i = 1; i <= cnt; ++ i)
{
count_edge = count_var = 0;
dfs(i);
count_var = mp.size();
if(!count_edge)
{
mp.clear();
continue;
}
if(count_edge / 2 >= count_var) ans += count_var;
else ans += count_var - 1;
mp.clear();
}
printf("Case #%d: %d\n",Case ++, ans);
}
}
2020牛客多校第8场i-Interesting Computer Game[图论签到]
最新推荐文章于 2021-09-23 20:57:39 发布