답안 #982476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982476 2024-05-14T09:43:46 Z shadow_sami 수도 (JOI20_capital_city) C++17
100 / 100
486 ms 69952 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
typedef map<ll,ll> mi;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef pair<ld,ld> pd;
#define ff first
#define ss second
#define srt(a) sort(a.begin(),a.end());
#define fip(k, n) for (ll i = k; i < n; i++)
#define fjp(k, n) for (ll j = k; j < n; j++)
#define fin(k, n) for (ll i = k; i >= n; i--)
#define fjn(k, n) for (ll j = k; j >= n; j--)
#define fp(k, n, m) for (ll k = n; k < m; k++)
#define fn(k, n, m) for (ll k = n; k >= m; k--)
#define ordered_set tree<pi, null_type,less< pi >, rb_tree_tag,tree_order_statistics_node_update>
#define totalOne(n) __builtin_popcount(n)
#define backZero(n) __builtin_ctzll(n)
#define frontZero(n) __builtin_clzll(n)
#define fx(k) for ( auto x : k )
#define test ll t;cin >> t;while (t--)
#define nli "\n"

// ==========================(debug)============================================================================================== //

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _printn(x); cerr << nli;
#else
#define debug(x)
#endif

void _printn(ll x){ cerr<<x<<" "; }
void _printn(int x){ cerr<<x<<" "; }
void _printn(ld x){ cerr<<x<<" "; }
void _printn(double x){ cerr<<x<<" "; }
void _printn(string x){ cerr<<x<<" "; }
void _printn(char x){ cerr<<x<<" "; }
void _printn(bool x){ cerr<<x<<" "; }
template<class T,class V>void _printn(pair<T,V> vv);
template<class T> void _printn(vector<T> vv);
template<class T> void _printn(set<T> vv);
template<class T,class V> void _printn(map<T,V> vv);
template<class T> void _printn(multiset<T> vv);
template<class T,class V>void _printn(pair<T,V> vv){ cerr<<"( ";_printn(vv.ff);cerr<<",";_printn(vv.ss);cerr<<")";}
template<class T> void _printn(vector<T> vv){ cerr<<"[ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"]"; };
template<class T> void _printn(set<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T> void _printn(multiset<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T,class V> void _printn(map<T,V> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };

// ==========================(debug)============================================================================================== //

ll n,m,tp,tp2,res,cnt,sum,tptp,ans;
const ll mx = 5e5+5;
const ll mod = 1e9+7;

// ==========================(MOD)=============================================================================================== //

ll mod_add(ll aa,ll bb){ return ((aa%mod)+(bb%mod))%mod; }
ll mod_minus(ll aa,ll bb){ return (((aa%mod)-(bb%mod))+10*mod)%mod; }
ll mod_mul(ll aa,ll bb){ return ((aa%mod)*(bb%mod))%mod; }
ll mod_power(ll aa,ll bb){ aa%=mod; ll empowered = 1; bb%=mod-1; while(bb > 0){ if(bb & 1) empowered = mod_mul(empowered,aa); bb = bb >> 1; aa = mod_mul(aa,aa); } return empowered; }
ll mod_divi(ll aa,ll bb){ aa=mod_mul(aa,mod_power(bb,mod-2)); return aa; }

// ==========================(MOD)=============================================================================================== //

bool f = false;
ll a[mx];
vi adj[mx];
ll up[mx];
vi lis[mx];
bool is2[mx];
bool is[mx];
bool mark[mx];
bool cc[mx];
queue<ll> q;
ll sz[mx];
vi v;

void emptify(){
	while(q.size())
		q.pop();
	return;
}

void dfs(ll u,ll par){
	v.push_back(u);
	up[u] = par;
	sz[u] = 1;
	fx(adj[u])
		if(x!=par && !mark[x]){
			dfs(x,u);
			sz[u] += sz[x];
		}
	return;
}

ll get(ll u,ll par,ll d){
	fx(adj[u])
		if(x!=par && !mark[x] && 2*sz[x]>d)
			return get(x,u,d);
	return u;
}

void divide(ll u){		
	dfs(u,u);
	u = get(u,u,sz[u]);
	v.clear();
	dfs(u,u);	
	mark[u] = 1;
	fx(v)
		cc[x] = 1;
	res = 0;
	emptify();
	q.push(a[u]); 
	f = 1;	
	while(q.size()){
		auto it = q.front();        				
		q.pop();
		if(is2[it])
			continue;
		is2[it] = 1;
		res++;
		fx(lis[it]){
			is[x] = 1;
			if(!cc[x]){
				f = 0;
				break;
			}
			if(a[up[x]] != a[x] && !is2[a[up[x]]])
				q.push(a[up[x]]);        			
		}        	
		if(!f)	        		
			break;
	}
	if(f)
		ans = min(ans,res-1);	
	fx(v){
		cc[x] = 0;
		is[x] = 0;
		is2[a[x]] = 0;		
	}
	fx(adj[u])
		if(!mark[x])
			divide(x);
	return;	
}



int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    // #ifndef ONLINE_JUDGE
        // freopen("input1.txt", "r", stdin);
        // freopen("output1.txt", "w", stdout);
        // freopen("error1.txt", "w", stderr);
    // #endif // ONLINE_JUDGE

        cin>>n>>m;
        ll sr,de;
        fip(0,n-1){
        	cin>>sr>>de;
        	adj[sr].push_back(de);
        	adj[de].push_back(sr);
        }
        fip(1,n+1)	{
        	cin>>a[i];
        	lis[a[i]].push_back(i);
        }
        ans = 1e18;
        divide(1);
        cout<<ans<<nli;
        
    cerr << "Time elapsed: " << setprecision(6) << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31068 KB Output is correct
2 Correct 6 ms 31068 KB Output is correct
3 Correct 6 ms 31068 KB Output is correct
4 Correct 6 ms 31064 KB Output is correct
5 Correct 7 ms 31068 KB Output is correct
6 Correct 6 ms 31064 KB Output is correct
7 Correct 6 ms 31064 KB Output is correct
8 Correct 6 ms 31068 KB Output is correct
9 Correct 7 ms 31208 KB Output is correct
10 Correct 6 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31068 KB Output is correct
2 Correct 6 ms 31068 KB Output is correct
3 Correct 6 ms 31068 KB Output is correct
4 Correct 6 ms 31064 KB Output is correct
5 Correct 7 ms 31068 KB Output is correct
6 Correct 6 ms 31064 KB Output is correct
7 Correct 6 ms 31064 KB Output is correct
8 Correct 6 ms 31068 KB Output is correct
9 Correct 7 ms 31208 KB Output is correct
10 Correct 6 ms 31068 KB Output is correct
11 Correct 7 ms 31324 KB Output is correct
12 Correct 7 ms 31324 KB Output is correct
13 Correct 7 ms 31324 KB Output is correct
14 Correct 8 ms 31324 KB Output is correct
15 Correct 7 ms 31324 KB Output is correct
16 Correct 7 ms 31324 KB Output is correct
17 Correct 7 ms 31444 KB Output is correct
18 Correct 7 ms 31324 KB Output is correct
19 Correct 7 ms 31320 KB Output is correct
20 Correct 7 ms 31324 KB Output is correct
21 Correct 7 ms 31324 KB Output is correct
22 Correct 7 ms 31324 KB Output is correct
23 Correct 8 ms 31324 KB Output is correct
24 Correct 7 ms 31448 KB Output is correct
25 Correct 7 ms 31324 KB Output is correct
26 Correct 8 ms 31324 KB Output is correct
27 Correct 8 ms 31576 KB Output is correct
28 Correct 8 ms 31576 KB Output is correct
29 Correct 7 ms 31324 KB Output is correct
30 Correct 8 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 64832 KB Output is correct
2 Correct 155 ms 66244 KB Output is correct
3 Correct 360 ms 65752 KB Output is correct
4 Correct 167 ms 64844 KB Output is correct
5 Correct 335 ms 61816 KB Output is correct
6 Correct 157 ms 65256 KB Output is correct
7 Correct 327 ms 61732 KB Output is correct
8 Correct 155 ms 64424 KB Output is correct
9 Correct 401 ms 61036 KB Output is correct
10 Correct 431 ms 62592 KB Output is correct
11 Correct 421 ms 65092 KB Output is correct
12 Correct 425 ms 66852 KB Output is correct
13 Correct 417 ms 62288 KB Output is correct
14 Correct 430 ms 68996 KB Output is correct
15 Correct 380 ms 68288 KB Output is correct
16 Correct 427 ms 63040 KB Output is correct
17 Correct 418 ms 62480 KB Output is correct
18 Correct 378 ms 62660 KB Output is correct
19 Correct 395 ms 67004 KB Output is correct
20 Correct 486 ms 68784 KB Output is correct
21 Correct 9 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31068 KB Output is correct
2 Correct 6 ms 31068 KB Output is correct
3 Correct 6 ms 31068 KB Output is correct
4 Correct 6 ms 31064 KB Output is correct
5 Correct 7 ms 31068 KB Output is correct
6 Correct 6 ms 31064 KB Output is correct
7 Correct 6 ms 31064 KB Output is correct
8 Correct 6 ms 31068 KB Output is correct
9 Correct 7 ms 31208 KB Output is correct
10 Correct 6 ms 31068 KB Output is correct
11 Correct 7 ms 31324 KB Output is correct
12 Correct 7 ms 31324 KB Output is correct
13 Correct 7 ms 31324 KB Output is correct
14 Correct 8 ms 31324 KB Output is correct
15 Correct 7 ms 31324 KB Output is correct
16 Correct 7 ms 31324 KB Output is correct
17 Correct 7 ms 31444 KB Output is correct
18 Correct 7 ms 31324 KB Output is correct
19 Correct 7 ms 31320 KB Output is correct
20 Correct 7 ms 31324 KB Output is correct
21 Correct 7 ms 31324 KB Output is correct
22 Correct 7 ms 31324 KB Output is correct
23 Correct 8 ms 31324 KB Output is correct
24 Correct 7 ms 31448 KB Output is correct
25 Correct 7 ms 31324 KB Output is correct
26 Correct 8 ms 31324 KB Output is correct
27 Correct 8 ms 31576 KB Output is correct
28 Correct 8 ms 31576 KB Output is correct
29 Correct 7 ms 31324 KB Output is correct
30 Correct 8 ms 31324 KB Output is correct
31 Correct 347 ms 64832 KB Output is correct
32 Correct 155 ms 66244 KB Output is correct
33 Correct 360 ms 65752 KB Output is correct
34 Correct 167 ms 64844 KB Output is correct
35 Correct 335 ms 61816 KB Output is correct
36 Correct 157 ms 65256 KB Output is correct
37 Correct 327 ms 61732 KB Output is correct
38 Correct 155 ms 64424 KB Output is correct
39 Correct 401 ms 61036 KB Output is correct
40 Correct 431 ms 62592 KB Output is correct
41 Correct 421 ms 65092 KB Output is correct
42 Correct 425 ms 66852 KB Output is correct
43 Correct 417 ms 62288 KB Output is correct
44 Correct 430 ms 68996 KB Output is correct
45 Correct 380 ms 68288 KB Output is correct
46 Correct 427 ms 63040 KB Output is correct
47 Correct 418 ms 62480 KB Output is correct
48 Correct 378 ms 62660 KB Output is correct
49 Correct 395 ms 67004 KB Output is correct
50 Correct 486 ms 68784 KB Output is correct
51 Correct 9 ms 31068 KB Output is correct
52 Correct 298 ms 51264 KB Output is correct
53 Correct 277 ms 51396 KB Output is correct
54 Correct 295 ms 55092 KB Output is correct
55 Correct 323 ms 51396 KB Output is correct
56 Correct 304 ms 51424 KB Output is correct
57 Correct 322 ms 51380 KB Output is correct
58 Correct 277 ms 56960 KB Output is correct
59 Correct 253 ms 58000 KB Output is correct
60 Correct 315 ms 56256 KB Output is correct
61 Correct 400 ms 56256 KB Output is correct
62 Correct 157 ms 69060 KB Output is correct
63 Correct 168 ms 68976 KB Output is correct
64 Correct 197 ms 69952 KB Output is correct
65 Correct 154 ms 69324 KB Output is correct
66 Correct 210 ms 63168 KB Output is correct
67 Correct 220 ms 62544 KB Output is correct
68 Correct 205 ms 61636 KB Output is correct
69 Correct 230 ms 61620 KB Output is correct
70 Correct 241 ms 62144 KB Output is correct
71 Correct 210 ms 61772 KB Output is correct
72 Correct 240 ms 61688 KB Output is correct
73 Correct 212 ms 60868 KB Output is correct
74 Correct 228 ms 62460 KB Output is correct
75 Correct 221 ms 62908 KB Output is correct
76 Correct 348 ms 61380 KB Output is correct
77 Correct 336 ms 56044 KB Output is correct
78 Correct 400 ms 62976 KB Output is correct
79 Correct 413 ms 61964 KB Output is correct
80 Correct 405 ms 68668 KB Output is correct
81 Correct 389 ms 65632 KB Output is correct
82 Correct 422 ms 64944 KB Output is correct
83 Correct 409 ms 61904 KB Output is correct
84 Correct 449 ms 66716 KB Output is correct
85 Correct 399 ms 66724 KB Output is correct
86 Correct 411 ms 60604 KB Output is correct
87 Correct 439 ms 62400 KB Output is correct
88 Correct 363 ms 63672 KB Output is correct
89 Correct 338 ms 60612 KB Output is correct
90 Correct 354 ms 60600 KB Output is correct
91 Correct 370 ms 63260 KB Output is correct
92 Correct 343 ms 61412 KB Output is correct
93 Correct 335 ms 60352 KB Output is correct
94 Correct 364 ms 59840 KB Output is correct
95 Correct 346 ms 61380 KB Output is correct
96 Correct 324 ms 61372 KB Output is correct
97 Correct 378 ms 62600 KB Output is correct