Submission #676766

# Submission time Handle Problem Language Result Execution time Memory
676766 2023-01-01T04:11:49 Z victor_gao Capital City (JOI20_capital_city) C++17
11 / 100
913 ms 524288 KB
//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,k,cap[N],dep[N],fa[N];
vector<int>g[N],col[N],G[N];
vector<pii>edge;
int dfn[N],low[N],dft=0,boss[N],val[N],dp[N],nscc=0,deg[N];
bool ins[N],vis[N];
stack<int>st;
void dfs(int p,int lp){
    fa[p]=lp;
    dep[p]=dep[lp]+1;
    for (auto i:g[p])
        if (i!=lp)
            dfs(i,p);
}
int jump(int a,int b,int c){
    while (a!=b){
      //  cout<<a<<" "<<b<<' '<<c<<'\n';
        if (dep[a]<dep[b]) swap(a,b);
        G[c].push_back(cap[a]);
       // cout<<"add "<<c<<" "<<cap[a]<<'\n';
        edge.push_back({c,cap[a]});
        a=fa[a];
    }
    G[c].push_back(cap[a]);
    edge.push_back({c,cap[a]});
    return a;
}
void tarjan(int p){
    dfn[p]=low[p]=++dft;
    st.push(p); ins[p]=1;
    for (auto i:G[p]){
        if (!dfn[i]){
            tarjan(i);
            low[p]=min(low[p],low[i]);
        }
        else if (ins[i]&&dfn[i]<dfn[p])
            low[p]=min(low[p],dfn[i]);
    }
    if (low[p]==dfn[p]){
        nscc++;
        for (int x=0;x!=p;st.pop()){
            x=st.top(); ins[x]=0;
            boss[x]=nscc;
            val[nscc]++;
        }
    }
}
void build(){
    for (int i=0;i<n;i++)
        G[i].clear();
    for (auto i:edge){
        if (boss[i.x]!=boss[i.y]){
            deg[boss[i.x]]++;
        }
    }
}
signed main(){
   // fast
    cin>>n>>k;
    for (int i=1;i<n;i++){
        int a,b; cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for (int i=1;i<=n;i++){
        cin>>cap[i];
        col[cap[i]].push_back(i);
    }
    dfs(1,0);
    for (int i=1;i<=k;i++){
        while (col[i].size()>1){
            int a=col[i].back(); col[i].pop_back();
            int b=col[i].back(); col[i].pop_back();
            col[i].push_back(jump(a,b,i));
        }
    }
    int ans=1e9;
    for (int i=1;i<=k;i++)
        if (!dfn[i])
            tarjan(i);
    build();
    for (int i=1;i<=k;i++)
        if (deg[boss[i]]==0)
            ans=min(ans,val[boss[i]]);
    cout<<ans-1<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14480 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 9 ms 14432 KB Output is correct
8 Correct 9 ms 14372 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14480 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 9 ms 14432 KB Output is correct
8 Correct 9 ms 14372 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 10 ms 15020 KB Output is correct
12 Correct 9 ms 15024 KB Output is correct
13 Correct 10 ms 15092 KB Output is correct
14 Correct 9 ms 15020 KB Output is correct
15 Correct 15 ms 16300 KB Output is correct
16 Correct 13 ms 17444 KB Output is correct
17 Correct 31 ms 39296 KB Output is correct
18 Correct 35 ms 39244 KB Output is correct
19 Correct 38 ms 39336 KB Output is correct
20 Correct 33 ms 39184 KB Output is correct
21 Correct 33 ms 39300 KB Output is correct
22 Correct 40 ms 40544 KB Output is correct
23 Correct 44 ms 40144 KB Output is correct
24 Correct 10 ms 14804 KB Output is correct
25 Correct 25 ms 25152 KB Output is correct
26 Correct 22 ms 25976 KB Output is correct
27 Correct 22 ms 25472 KB Output is correct
28 Correct 21 ms 25808 KB Output is correct
29 Correct 22 ms 25444 KB Output is correct
30 Correct 21 ms 25672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 913 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14480 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 9 ms 14432 KB Output is correct
8 Correct 9 ms 14372 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 10 ms 15020 KB Output is correct
12 Correct 9 ms 15024 KB Output is correct
13 Correct 10 ms 15092 KB Output is correct
14 Correct 9 ms 15020 KB Output is correct
15 Correct 15 ms 16300 KB Output is correct
16 Correct 13 ms 17444 KB Output is correct
17 Correct 31 ms 39296 KB Output is correct
18 Correct 35 ms 39244 KB Output is correct
19 Correct 38 ms 39336 KB Output is correct
20 Correct 33 ms 39184 KB Output is correct
21 Correct 33 ms 39300 KB Output is correct
22 Correct 40 ms 40544 KB Output is correct
23 Correct 44 ms 40144 KB Output is correct
24 Correct 10 ms 14804 KB Output is correct
25 Correct 25 ms 25152 KB Output is correct
26 Correct 22 ms 25976 KB Output is correct
27 Correct 22 ms 25472 KB Output is correct
28 Correct 21 ms 25808 KB Output is correct
29 Correct 22 ms 25444 KB Output is correct
30 Correct 21 ms 25672 KB Output is correct
31 Runtime error 913 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -