Submission #743283

# Submission time Handle Problem Language Result Execution time Memory
743283 2023-05-17T09:28:50 Z myrcella Mergers (JOI19_mergers) C++17
0 / 100
3000 ms 29092 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);
	int tmp = col[u];
	if (dep[u]<dep[v]) swap(u,v);
	while (dep[u]>dep[v]) u = par[u],merge(col[u],tmp);
	while (u!=v) {
		u = par[u],merge(col[u],tmp);
		v = par[v],merge(col[v],tmp);
	}
	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 = -1;
	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<<max(cnt,0);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3031 ms 29092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23792 KB Output isn't correct
2 Halted 0 ms 0 KB -