답안 #943838

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943838 2024-03-12T02:09:10 Z guagua0407 수도 (JOI20_capital_city) C++17
0 / 100
262 ms 36124 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=2e5+5;
const int inf=1e9;
vector<int> adj[mxn];
bool visited[mxn];
int sz[mxn];
vector<int> belong[mxn];
bool now[mxn];
int par[mxn];
int c[mxn];
bool did[mxn];
int ans=inf;
vector<int> st;

int dfs(int v,int p=0){
    sz[v]=1;
    st.push_back(v);
    now[v]=true;
    par[v]=p;
    for(auto u:adj[v]){
        if(visited[u] or u==p) continue;
        sz[v]+=dfs(u,v);
    }
    return sz[v];
}

int find(int v,int val,int p=0){
    for(auto u:adj[v]){
        if(visited[u] or u==p) continue;
        if(sz[u]*2>val) return find(u,val,v);
    }
    return v;
}

void centroid(int v){
    v=find(v,dfs(v));
    queue<int> q;
    vector<int> tmp;
    q.push(c[v]);
    did[c[v]]=true;
    tmp.push_back(c[v]);
    bool ok=true;
    while(!q.empty()){
        int C=q.front();
        q.pop();
        for(auto x:belong[C]){
            if(!now[x]){
                ok=false;
                break;
            }
            if(par[x]!=0 and !did[c[par[x]]]){
                did[c[par[x]]]=true;
                tmp.push_back(c[par[x]]);
                q.push(c[par[x]]);
            }
        }
        if(!ok){
            break;
        }
    }
    for(auto C:tmp){
        did[C]=false;
    }
    for(auto x:st){
        now[x]=false;
    }
    st.clear();
    if(ok){
        ans=min(ans,(int)tmp.size()-1);
    }
    visited[v]=true;
    for(auto u:adj[v]){
        if(!visited[u]){
            centroid(u);
        }
    }
}

int main() {_
    int n,k;
    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];
        belong[c[i]].push_back(i);
    }
    centroid(1);
    cout<<ans<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12380 KB Output is correct
2 Correct 4 ms 12380 KB Output is correct
3 Correct 4 ms 12380 KB Output is correct
4 Incorrect 4 ms 12380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12380 KB Output is correct
2 Correct 4 ms 12380 KB Output is correct
3 Correct 4 ms 12380 KB Output is correct
4 Incorrect 4 ms 12380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 262 ms 36124 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12380 KB Output is correct
2 Correct 4 ms 12380 KB Output is correct
3 Correct 4 ms 12380 KB Output is correct
4 Incorrect 4 ms 12380 KB Output isn't correct
5 Halted 0 ms 0 KB -