Submission #941128

# Submission time Handle Problem Language Result Execution time Memory
941128 2024-03-08T07:41:18 Z guagua0407 Capital City (JOI20_capital_city) C++17
0 / 100
24 ms 32860 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2000+5;
const int inf=1e9;
vector<int> adj[mxn],adj2[mxn],adjr[mxn];
bool no[mxn];
int c[mxn];
vector<vector<int>> belong(mxn);
int n,k;
vector<vector<int>> mp(mxn,vector<int>(mxn));
vector<bool> did(mxn);
vector<int> ord;
vector<bool> visited(mxn);
int cnt[mxn];
int scc[mxn];
int in[mxn];
int top[mxn];

void dfs(int v){
    visited[v]=true;
    for(auto u:adj2[v]){
        if(visited[u]) continue;
        dfs(u);
    }
    ord.push_back(v);
}

void dfsr(int v,int id){
    visited[v]=true;
    scc[v]=id;
    cnt[id]++;
    for(auto u:adjr[v]){
        if(visited[u]) continue;
        dfsr(u,id);
    }
}

void dfs2(int v,int p=0){
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs2(u,v);
        for(int i=0;i<k;i++){
            mp[v][i]+=mp[u][i];
        }
    }
    mp[v][c[v]]++;
    for(int i=0;i<k;i++){
        if(mp[v][i]==(int)belong[i].size() and top[i]==0) top[i]=v;
    }
}

void dfs3(int v,int C,int p=0){
    int x=c[v];
    if(x!=C and !did[x]){
        did[x]=true;
        for(auto u:belong[x]){
            if(no[u]){
                adj2[C].push_back(x);
                adjr[x].push_back(C);
                break;
            }
        }
    }
    for(auto u:adj[v]){
        if(u==p or no[u]) continue;
        dfs3(u,C,v);
    }
}

int main() {_
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        cin>>c[i];
        c[i]--;
        belong[c[i]].push_back(i);
    }
    dfs2(1);
    for(int i=0;i<k;i++){
        for(int j=1;j<=n;j++){
            no[j]=false;
            if(mp[j][i]==0 or mp[j][i]==mp[1][i] and j!=top[i]){
                no[j]=true;
            }
        }
        for(int j=0;j<k;j++){
            did[j]=false;
        }
        /*cout<<i+1<<'\n';
        for(int j=1;j<=n;j++){
            if(!no[j]) cout<<j<<' ';
        }
        cout<<'\n';*/
        for(int j=1;j<=n;j++){
            if(!no[j]){
                dfs3(j,i);
                break;
            }
        }
    }
    for(int i=0;i<k;i++){
        if(!visited[i]){
            dfs(i);
        }
    }
    reverse(all(ord));
    fill(all(visited),false);
    int sccnt=0;
    for(int i=0;i<k;i++){
        if(!visited[ord[i]]){
            dfsr(ord[i],sccnt);
            sccnt++;
        }
    }
    for(int i=0;i<k;i++){
        for(auto u:adj2[i]){
            //cout<<i+1<<' '<<u+1<<'\n';
            if(scc[i]==scc[u]) continue;
            in[scc[i]]++;
        }
    }
    int ans=inf;
    for(int i=0;i<sccnt;i++){
        if(in[i]==0) ans=min(ans,cnt[i]-1);
    }
    cout<<ans<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:103:50: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  103 |             if(mp[j][i]==0 or mp[j][i]==mp[1][i] and j!=top[i]){
capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16476 KB Output is correct
2 Correct 8 ms 16332 KB Output is correct
3 Correct 9 ms 16472 KB Output is correct
4 Incorrect 8 ms 16476 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16476 KB Output is correct
2 Correct 8 ms 16332 KB Output is correct
3 Correct 9 ms 16472 KB Output is correct
4 Incorrect 8 ms 16476 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 32860 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16476 KB Output is correct
2 Correct 8 ms 16332 KB Output is correct
3 Correct 9 ms 16472 KB Output is correct
4 Incorrect 8 ms 16476 KB Output isn't correct
5 Halted 0 ms 0 KB -