답안 #743339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
743339 2023-05-17T09:58:38 Z myrcella Mergers (JOI19_mergers) C++17
0 / 100
175 ms 38604 KB
//by szh
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}

const int maxn = 5e5+10;

int n,m;
vector <int> edge[maxn];
int dep[maxn];
int f[maxn];
int deg[maxn];
vector <int> node[maxn];
int col[maxn];
int par[maxn];

void dfs(int u,int fa) {
	par[u] = fa;
	dep[u] = dep[fa] + 1;
	for (int v:edge[u]) {
		if (v==fa) continue;
		dfs(v,u);
	}
	return;
}

int getf(int x) {
	if (f[x]==x) return x;
	else return f[x] = getf(f[x]);
}

void merge(int x,int y) {
	f[getf(x)] = getf(y);
}

void update(int u,int v) {
//	debug(u),debug(v);
	vector <int> path;
	int tmp = col[u];
	if (dep[u]<dep[v]) swap(u,v);
	while (dep[u]>dep[v]) path.pb(u),u = par[u],merge(col[u],tmp);
	while (u!=v) {
		path.pb(u);path.pb(v);
		u = par[u],merge(col[u],tmp);
		v = par[v],merge(col[v],tmp);
	}
	while (!path.empty()) par[path.back()] = u,path.pop_back();
	return;
}

map <pii,int> mp;

int main() {
//	freopen("input.txt","r",stdin);	
	std::ios::sync_with_stdio(false);cin.tie(0);
	cin>>n>>m;
	rep(i,1,m+1) f[i] = i;
	rep(i,1,n) {
		int u,v;
		cin>>u>>v;
		edge[u].pb(v);
		edge[v].pb(u);
	}
	dfs(1,0);
	rep(i,1,n+1) {
		int c;cin>>c;
		col[i] = c;
		node[c].pb(i);
	}
//	rep(i,1,n+1)
//		rep(j,1,i) if (col[i]==col[j]) update(i,j);
	rep(i,1,m+1) {
		if (node[i].empty()) continue;
		int tmp = node[i][0];
		rep(j,1,SZ(node[i])) update(tmp,node[i][j]);
	}
	rep(i,1,n+1) 
		for (int v:edge[i]) {
			int fx = getf(col[i]), fy = getf(col[v]);
			if (fx==fy) continue;
			if (fx>fy) swap(fx,fy);
			if (mp.find({fx,fy})!=mp.end()) continue;
			mp[{fx,fy}]=1;
			deg[fx]++;
			deg[fy]++;
		}
	int hii = 0;
	int cnt = 0;
	rep(i,1,m+1) {
		if (getf(i)==i) hii++;
		if (getf(i)==i and deg[i]==1) cnt++;
	}
	assert(SZ(mp)==hii-1);
	cout<<(cnt+1)/2;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Incorrect 14 ms 23748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Incorrect 14 ms 23748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Incorrect 14 ms 23748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 29032 KB Output is correct
2 Correct 175 ms 38604 KB Output is correct
3 Correct 15 ms 24028 KB Output is correct
4 Incorrect 17 ms 23952 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Incorrect 14 ms 23748 KB Output isn't correct
3 Halted 0 ms 0 KB -