Submission #946392

# Submission time Handle Problem Language Result Execution time Memory
946392 2024-03-14T15:43:04 Z yeediot Capital City (JOI20_capital_city) C++14
100 / 100
484 ms 45536 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
    int a,b;
    int operator()(const int x)const{
        return a*x+b;
    }
};
bool check(line l1,line l2,line l3){
    return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=2e5+5;
vector<int>adj[mxn];
vector<int>g[mxn];
int c[mxn];
bool vis[mxn];
int cnt[mxn];
bool used[mxn];
bool in[mxn];
int p[mxn];
vector<int>sir;
void cnt_sz(int v,int pa){
    cnt[v]=1;
    in[v]=1;
    sir.pb(v);
    for(auto u:adj[v]){
        if(u==pa or vis[u])continue;
        cnt_sz(u,v);
        cnt[v]+=cnt[u];
    }
}
int find(int v,int pa,int tar){
    for(auto u:adj[v]){
        if(vis[u] or u==pa)continue;
        if(cnt[u]*2>tar)return find(u,v,tar);
    }
    return v;
}
void calc_pa(int v,int pa){
    p[v]=pa;
    for(auto u:adj[v]){
        if(vis[u] or u==pa)continue;
        calc_pa(u,v);
    }
}
int ans;
void cen(int v){
    cnt_sz(v,v);
    v=find(v,v,cnt[v]);
    calc_pa(v,v);
    debug(v);
    vis[v]=1;
    queue<int>q;
    q.push(c[v]);
    used[c[v]]=1;
    vector<int>undo;
    undo.pb(c[v]);
    while(sz(q)){
        int color=q.front();
        q.pop();
        for(auto u:g[color]){
            if(!in[u]){
                goto done;
            }
            if(p[u]!=u and !used[c[p[u]]]){
                q.push(c[p[u]]);
                used[c[p[u]]]=1;
                undo.pb(c[p[u]]);
            }
        }
    }
    chmin(ans,sz(undo)-1);
    done:
    while(sz(undo)){
        used[undo.back()]=0;
        undo.pop_back();
    }
    while(sz(sir)){
        in[sir.back()]=0;
        sir.pop_back();
    }
    for(auto u:adj[v]){
        if(vis[u])continue;
        cen(u);
    }
}
signed main(){
    setio();
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    for(int i=1;i<=n;i++){
        cin>>c[i];
        g[c[i]].pb(i);
    }
    ans=k-1;
    cen(1);
    cout<<ans<<'\n';
}
 /*
 input:
 
 */















 

Compilation message

capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14772 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 15092 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14772 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 15092 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 4 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14936 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 15196 KB Output is correct
19 Correct 5 ms 15196 KB Output is correct
20 Correct 4 ms 15196 KB Output is correct
21 Correct 4 ms 15196 KB Output is correct
22 Correct 5 ms 15196 KB Output is correct
23 Correct 4 ms 15196 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 4 ms 15196 KB Output is correct
26 Correct 4 ms 15192 KB Output is correct
27 Correct 4 ms 15196 KB Output is correct
28 Correct 6 ms 15192 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 4 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 41160 KB Output is correct
2 Correct 146 ms 45536 KB Output is correct
3 Correct 319 ms 44488 KB Output is correct
4 Correct 160 ms 45508 KB Output is correct
5 Correct 333 ms 41800 KB Output is correct
6 Correct 151 ms 45380 KB Output is correct
7 Correct 300 ms 42392 KB Output is correct
8 Correct 153 ms 45332 KB Output is correct
9 Correct 405 ms 41148 KB Output is correct
10 Correct 438 ms 38856 KB Output is correct
11 Correct 422 ms 41540 KB Output is correct
12 Correct 398 ms 44156 KB Output is correct
13 Correct 484 ms 38340 KB Output is correct
14 Correct 414 ms 44572 KB Output is correct
15 Correct 426 ms 44340 KB Output is correct
16 Correct 415 ms 39200 KB Output is correct
17 Correct 436 ms 39936 KB Output is correct
18 Correct 430 ms 40016 KB Output is correct
19 Correct 461 ms 43212 KB Output is correct
20 Correct 428 ms 45108 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14772 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 15092 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 4 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14936 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 15196 KB Output is correct
19 Correct 5 ms 15196 KB Output is correct
20 Correct 4 ms 15196 KB Output is correct
21 Correct 4 ms 15196 KB Output is correct
22 Correct 5 ms 15196 KB Output is correct
23 Correct 4 ms 15196 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 4 ms 15196 KB Output is correct
26 Correct 4 ms 15192 KB Output is correct
27 Correct 4 ms 15196 KB Output is correct
28 Correct 6 ms 15192 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 4 ms 15196 KB Output is correct
31 Correct 299 ms 41160 KB Output is correct
32 Correct 146 ms 45536 KB Output is correct
33 Correct 319 ms 44488 KB Output is correct
34 Correct 160 ms 45508 KB Output is correct
35 Correct 333 ms 41800 KB Output is correct
36 Correct 151 ms 45380 KB Output is correct
37 Correct 300 ms 42392 KB Output is correct
38 Correct 153 ms 45332 KB Output is correct
39 Correct 405 ms 41148 KB Output is correct
40 Correct 438 ms 38856 KB Output is correct
41 Correct 422 ms 41540 KB Output is correct
42 Correct 398 ms 44156 KB Output is correct
43 Correct 484 ms 38340 KB Output is correct
44 Correct 414 ms 44572 KB Output is correct
45 Correct 426 ms 44340 KB Output is correct
46 Correct 415 ms 39200 KB Output is correct
47 Correct 436 ms 39936 KB Output is correct
48 Correct 430 ms 40016 KB Output is correct
49 Correct 461 ms 43212 KB Output is correct
50 Correct 428 ms 45108 KB Output is correct
51 Correct 3 ms 14936 KB Output is correct
52 Correct 307 ms 30788 KB Output is correct
53 Correct 334 ms 30664 KB Output is correct
54 Correct 309 ms 30728 KB Output is correct
55 Correct 306 ms 30744 KB Output is correct
56 Correct 281 ms 30656 KB Output is correct
57 Correct 327 ms 30616 KB Output is correct
58 Correct 299 ms 34168 KB Output is correct
59 Correct 268 ms 34768 KB Output is correct
60 Correct 316 ms 33148 KB Output is correct
61 Correct 308 ms 32908 KB Output is correct
62 Correct 143 ms 45528 KB Output is correct
63 Correct 140 ms 45508 KB Output is correct
64 Correct 140 ms 45524 KB Output is correct
65 Correct 144 ms 45512 KB Output is correct
66 Correct 218 ms 39056 KB Output is correct
67 Correct 202 ms 38592 KB Output is correct
68 Correct 205 ms 38964 KB Output is correct
69 Correct 209 ms 38852 KB Output is correct
70 Correct 258 ms 38936 KB Output is correct
71 Correct 232 ms 38852 KB Output is correct
72 Correct 252 ms 38848 KB Output is correct
73 Correct 187 ms 38080 KB Output is correct
74 Correct 210 ms 38852 KB Output is correct
75 Correct 216 ms 38808 KB Output is correct
76 Correct 295 ms 37644 KB Output is correct
77 Correct 305 ms 35776 KB Output is correct
78 Correct 346 ms 39912 KB Output is correct
79 Correct 347 ms 37820 KB Output is correct
80 Correct 413 ms 44856 KB Output is correct
81 Correct 391 ms 41412 KB Output is correct
82 Correct 340 ms 41408 KB Output is correct
83 Correct 427 ms 38080 KB Output is correct
84 Correct 361 ms 44204 KB Output is correct
85 Correct 391 ms 42380 KB Output is correct
86 Correct 380 ms 37772 KB Output is correct
87 Correct 329 ms 39592 KB Output is correct
88 Correct 303 ms 40808 KB Output is correct
89 Correct 304 ms 36816 KB Output is correct
90 Correct 346 ms 36536 KB Output is correct
91 Correct 329 ms 39104 KB Output is correct
92 Correct 381 ms 37620 KB Output is correct
93 Correct 290 ms 37372 KB Output is correct
94 Correct 287 ms 36652 KB Output is correct
95 Correct 305 ms 38288 KB Output is correct
96 Correct 337 ms 36980 KB Output is correct
97 Correct 295 ms 39108 KB Output is correct