제출 #1170639

#제출 시각아이디문제언어결과실행 시간메모리
1170639SmuggingSpunCapital City (JOI20_capital_city)C++20
11 / 100
39 ms780 KiB
#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
template<class T>void minimize(T& a, T b){
    if(a > b){
        a = b;
    }
}
int n, k;
namespace sub1{
    void solve(){
        vector<vector<int>>g(n + 1);
        for(int i = 1; i < n; i++){
            int u, v;
            cin >> u >> v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }
        vector<int>color(n + 1), cnt_color(k, 0);
        for(int i = 1; i <= n; i++){
            cin >> color[i];
            cnt_color[--color[i]]++;
        }
        int ans = n;
        for(int mask = (1 << k) - 1; mask > 0; mask--){
            if(__builtin_popcount(mask) < ans){
                queue<int>q;
                vector<bool>vis(n + 1, false);
                for(int i = 1; i <= n; i++){
                    if(1 << color[i] & mask){
                        q.push(i);
                        vis[i] = true;
                        break;
                    }
                }
                while(!q.empty()){
                    int u = q.front();
                    q.pop();
                    for(int& v : g[u]){
                        if(!vis[v] && (1 << color[v] & mask)){
                            vis[v] = true;
                            q.push(v);
                        }
                    }
                }
                int expect = 0;
                for(int i = 0; i < k; i++){
                    if(1 << i & mask){
                        expect += cnt_color[i];
                    }
                }
                if(count(vis.begin(), vis.end(), true) == expect){
                    ans = __builtin_popcount(mask);
                }
            }
        }
        cout << ans - 1;
    }
}
namespace sub2{
    void solve(){
        vector<vector<int>>g(n + 1), vc(k + 1);
        for(int i = 1; i < n; i++){
            int u, v;
            cin >> u >> v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }
        vector<int>color(n + 1);
        for(int i = 1; i <= n; i++){
            cin >> color[i];
            vc[color[i]].emplace_back(i);
        }
        vector<int>parent(n + 1);
        function<void(int)>dfs;
        dfs = [&] (int s){
            for(int& d : g[s]){
                if(d != parent[s]){
                    parent[d] = s;
                    dfs(d);
                }
            }
        };
        int ans = n;
        for(int i = 1; i <= k; i++){
            int root = vc[i][0];
            dfs(parent[root] = root);
            queue<int>q;
            for(int& x : vc[i]){
                q.push(x);
            }
            vector<bool>vis_c(k + 1, false), vis(n + 1, false);
            vis[root] = true;
            vis_c[i] = true;
            while(!q.empty()){
                int u = q.front();
                q.pop();
                while(!vis[u = parent[u]]){
                    if(!vis_c[color[u]]){
                        for(int& x : vc[color[u]]){
                            q.push(x);
                        }
                        vis_c[color[u]] = true;
                    }
                    vis[u] = true;
                }
            }
            minimize(ans, int(count(vis_c.begin(), vis_c.end(), true)));
        }
        cout << ans - 1;
    }
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
    cin >> n >> k;
    if(n <= 20){
        sub1::solve();
    }
    else if(n <= 2000){
        sub2::solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

capital_city.cpp: In function 'int main()':
capital_city.cpp:116:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...