답안 #152121

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
152121 2019-09-06T14:48:55 Z ryansee Mergers (JOI19_mergers) C++14
0 / 100
140 ms 58428 KB
#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
// #define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second	
#define db 0
#define EPS (1e-7)    //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (500006)
#define ll /*long long*/ int 
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define MSB(bm) ((bm==0)?-1:(63-__builtin_clzll((bm))))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
// #define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
// #define degug(...) 663
#define cerr if(0)cout
#endif
#warning "// FOR has been inclusive now!!!!"
ll n, k, S[MAXN], sz[MAXN];
vector<int> v[MAXN], g[MAXN];
multiset<ll> app[MAXN];
stack <pi> stk;
struct ufds_ {
	int p[MAXN];
	ufds_ () { FOR(i,0,MAXN-1) p[i]=i; }
	void merge(ll x,ll y) {
		p[find(x)]=find(y);
	}
	ll find(ll x){return (p[x]==x)?x:p[x]=find(p[x]); }
	bool same(ll a,ll b){return find(a)==find(b);}
} ufds;
void merge(ll x,ll y) {
	for(auto i:app[x]) app[y].ins(i);
	app[x].clear();
	ufds.merge(x,y);
}
void dfs(ll x, ll p) {
	while(app[ufds.find(S[x])].size() && stk.size() >= *app[ufds.find(S[x])].begin()) {
		if(ufds.same(stk.top().f, S[x])) {
			app[ufds.find(S[x])].erase(app[ufds.find(S[x])].find(stk.size()));
		} else {
			if(app[ufds.find(S[x])].size() >= app[ufds.find(stk.top().f)].size()) {
				merge(ufds.find(stk.top().f), ufds.find(S[x]));
			} else {
				merge(ufds.find(S[x]), ufds.find(stk.top().f));
			}
		}
		stk.pop();
	}
	stk.emplace(ufds.find(S[x]), x);
	app[ufds.find(S[x])].ins(stk.size());
	sz[x]=stk.size();
	for(auto i:v[x]) if(i^p){
		dfs(i, x);
	}
	if(stk.size()&&stk.top().s == x) {
		stk.pop();
		app[ufds.find(S[x])].erase(app[ufds.find(S[x])].find(sz[x]));
	}
}
vector<pi> e;
int pr[MAXN];
void tongli(){
	mmst(pr,-1);
	FOR(i,1,n) {
		if(~pr[S[i]]) {
			ufds.merge(i, pr[S[i]]);
		} pr[S[i]]=i;
	}
}
int main()
{
	FAST
	cin>>n>>k;
	FOR(i,1,n-1) {
		ll a, b;cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
		e.eb(a,b);
	}
	FOR(i,1,n) cin>>S[i]; tongli();
	dfs(1,1); vector<int> deg(n+1, 0);
	for(auto i:e) {
		ll x=ufds.find(i.f), y=ufds.find(i.s);
		if(x==y) continue;
		++deg[x], ++deg[y];
	}
	ll leaves=0; FOR(i,1,n) if(ufds.find(i)==i && deg[i]==1) ++ leaves;
// 	cout<<(ll)ceill(leaves/(ld)2)<<'\n';
cout<<max(0,leaves-1);
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
*/

Compilation message

mergers.cpp:42:2: warning: #warning "// FOR has been inclusive now!!!!" [-Wcpp]
 #warning "// FOR has been inclusive now!!!!"
  ^~~~~~~
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:62:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(app[ufds.find(S[x])].size() && stk.size() >= *app[ufds.find(S[x])].begin()) {
                                       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:19:25: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
 #define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
                         ^
mergers.cpp:104:2: note: in expansion of macro 'FOR'
  FOR(i,1,n) cin>>S[i]; tongli();
  ^~~
mergers.cpp:104:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  FOR(i,1,n) cin>>S[i]; tongli();
                        ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 51320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 51320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 51320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 127 ms 58224 KB Output is correct
2 Incorrect 140 ms 58428 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 51320 KB Output isn't correct
2 Halted 0 ms 0 KB -