답안 #318382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
318382 2020-11-01T16:16:39 Z LifeHappen__ 수도 (JOI20_capital_city) C++14
100 / 100
770 ms 50036 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
 
#define pb push_back
#define mp make_pair
#define taskname "A"
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
 
const int maxn = 2e5 + 10;
int n , k;
int c[maxn] , cnt_col[maxn];
vector<int> col_node[maxn];
vector<int> adj[maxn];
int sub[maxn],big[maxn];
int vis[maxn];
int vis1[maxn];
 
int FindRoot(int u){
    function<void(int,int)> dfs = [&](int u , int v){
        sub[u] = 1;big[u] = -1;
        for(auto c : adj[u]){
            if(c != v && vis[c] == 0){
                dfs(c , u);
                sub[u] += sub[c];
                if(big[u] == -1 || sub[big[u]] < sub[c])big[u] = c;
            }
        }
    };
    dfs(u , 0);
    int tot = sub[u];
    while(big[u] != -1 && sub[big[u]] * 2 >= tot)u = big[u];
    return u;
}
 
int pa[maxn];
int res = 1e9;
bool vis_col[maxn];
 
void centroid(int u){
    u = FindRoot(u);
    int root = u;
    pa[u] = u;
    function<void(int,int)> dfs = [&](int u , int v){
        vis1[u] = root;
        for(auto c : adj[u]){
            if(c != v && vis[c] == 0){
                pa[c] = u;
                dfs(c , u);
            }
        }
    };
    dfs(u , 0);
    vector<int> ans;
    queue<int> q;q.push(u);
    int cnt = -1;
    bool ok = 1;
    while(q.size() && ok == 1){
        int u = q.front();q.pop();
        ++cnt;
        while(ok == 1){
            if(vis1[u] == 0)break;
            if(vis_col[c[u]] == 0){
                cnt -= cnt_col[c[u]];
                vis_col[c[u]] = 1;
                ans.pb(c[u]);
                for(auto &v : col_node[c[u]]){
                    q.push(v);
                    if(vis1[v] != root){
                        ok = 0;break;
                    }
                }
            }
            vis1[u] = 0;
            u = pa[u];
            if(u == root)break;
        }
    }
    for(auto c : ans){
        vis_col[c] = 0;
    }
    if(cnt == 0){
        res = min(res , (int)ans.size() - 1);
    }
    vis[u] = 1;
    for(auto c : adj[u]){
        if(vis[c] == 0)centroid(c);
    }
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen(taskname".INP","r")){
		freopen(taskname".INP", "r",stdin);
		freopen(taskname".OUT", "w",stdout);
    }
    cin >> n >> k;
    for(int i = 1 ; i < n ; ++i){
        int u , v;cin >> u >> v;
        adj[u].pb(v);adj[v].pb(u);
    }
    for(int i = 1 ; i <= n ; ++i){
        cin >> c[i];
        cnt_col[c[i]]++;
        col_node[c[i]].pb(i);
    }
    centroid(1);
    cout << res;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:103:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  103 |   freopen(taskname".INP", "r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:104:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  104 |   freopen(taskname".OUT", "w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9964 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9964 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 9 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 9 ms 9964 KB Output is correct
15 Correct 9 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 10092 KB Output is correct
20 Correct 8 ms 10092 KB Output is correct
21 Correct 8 ms 10220 KB Output is correct
22 Correct 8 ms 10220 KB Output is correct
23 Correct 9 ms 10092 KB Output is correct
24 Correct 9 ms 9964 KB Output is correct
25 Correct 9 ms 10092 KB Output is correct
26 Correct 9 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 10092 KB Output is correct
29 Correct 9 ms 10092 KB Output is correct
30 Correct 9 ms 10092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 670 ms 49252 KB Output is correct
2 Correct 241 ms 50020 KB Output is correct
3 Correct 662 ms 48868 KB Output is correct
4 Correct 243 ms 49892 KB Output is correct
5 Correct 676 ms 45028 KB Output is correct
6 Correct 246 ms 49896 KB Output is correct
7 Correct 658 ms 45376 KB Output is correct
8 Correct 245 ms 50036 KB Output is correct
9 Correct 716 ms 43364 KB Output is correct
10 Correct 725 ms 39936 KB Output is correct
11 Correct 704 ms 44072 KB Output is correct
12 Correct 730 ms 47708 KB Output is correct
13 Correct 723 ms 39448 KB Output is correct
14 Correct 715 ms 48036 KB Output is correct
15 Correct 740 ms 47604 KB Output is correct
16 Correct 718 ms 40864 KB Output is correct
17 Correct 716 ms 41600 KB Output is correct
18 Correct 718 ms 41764 KB Output is correct
19 Correct 739 ms 46236 KB Output is correct
20 Correct 729 ms 49336 KB Output is correct
21 Correct 6 ms 9836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9964 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 9 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 9 ms 9964 KB Output is correct
15 Correct 9 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 10092 KB Output is correct
20 Correct 8 ms 10092 KB Output is correct
21 Correct 8 ms 10220 KB Output is correct
22 Correct 8 ms 10220 KB Output is correct
23 Correct 9 ms 10092 KB Output is correct
24 Correct 9 ms 9964 KB Output is correct
25 Correct 9 ms 10092 KB Output is correct
26 Correct 9 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 10092 KB Output is correct
29 Correct 9 ms 10092 KB Output is correct
30 Correct 9 ms 10092 KB Output is correct
31 Correct 670 ms 49252 KB Output is correct
32 Correct 241 ms 50020 KB Output is correct
33 Correct 662 ms 48868 KB Output is correct
34 Correct 243 ms 49892 KB Output is correct
35 Correct 676 ms 45028 KB Output is correct
36 Correct 246 ms 49896 KB Output is correct
37 Correct 658 ms 45376 KB Output is correct
38 Correct 245 ms 50036 KB Output is correct
39 Correct 716 ms 43364 KB Output is correct
40 Correct 725 ms 39936 KB Output is correct
41 Correct 704 ms 44072 KB Output is correct
42 Correct 730 ms 47708 KB Output is correct
43 Correct 723 ms 39448 KB Output is correct
44 Correct 715 ms 48036 KB Output is correct
45 Correct 740 ms 47604 KB Output is correct
46 Correct 718 ms 40864 KB Output is correct
47 Correct 716 ms 41600 KB Output is correct
48 Correct 718 ms 41764 KB Output is correct
49 Correct 739 ms 46236 KB Output is correct
50 Correct 729 ms 49336 KB Output is correct
51 Correct 6 ms 9836 KB Output is correct
52 Correct 581 ms 26916 KB Output is correct
53 Correct 603 ms 26916 KB Output is correct
54 Correct 603 ms 26980 KB Output is correct
55 Correct 620 ms 26916 KB Output is correct
56 Correct 606 ms 26912 KB Output is correct
57 Correct 582 ms 27060 KB Output is correct
58 Correct 528 ms 31460 KB Output is correct
59 Correct 547 ms 32100 KB Output is correct
60 Correct 688 ms 30692 KB Output is correct
61 Correct 669 ms 30184 KB Output is correct
62 Correct 241 ms 50020 KB Output is correct
63 Correct 239 ms 50020 KB Output is correct
64 Correct 243 ms 49892 KB Output is correct
65 Correct 242 ms 50020 KB Output is correct
66 Correct 422 ms 38616 KB Output is correct
67 Correct 423 ms 38472 KB Output is correct
68 Correct 412 ms 38360 KB Output is correct
69 Correct 425 ms 38620 KB Output is correct
70 Correct 427 ms 38492 KB Output is correct
71 Correct 426 ms 38492 KB Output is correct
72 Correct 417 ms 38492 KB Output is correct
73 Correct 425 ms 37256 KB Output is correct
74 Correct 417 ms 38584 KB Output is correct
75 Correct 436 ms 38364 KB Output is correct
76 Correct 658 ms 36528 KB Output is correct
77 Correct 689 ms 33636 KB Output is correct
78 Correct 768 ms 41556 KB Output is correct
79 Correct 741 ms 38560 KB Output is correct
80 Correct 738 ms 48536 KB Output is correct
81 Correct 747 ms 43744 KB Output is correct
82 Correct 745 ms 43880 KB Output is correct
83 Correct 761 ms 39044 KB Output is correct
84 Correct 770 ms 47580 KB Output is correct
85 Correct 759 ms 45080 KB Output is correct
86 Correct 758 ms 38656 KB Output is correct
87 Correct 733 ms 41100 KB Output is correct
88 Correct 673 ms 42468 KB Output is correct
89 Correct 663 ms 36836 KB Output is correct
90 Correct 655 ms 36708 KB Output is correct
91 Correct 663 ms 39908 KB Output is correct
92 Correct 655 ms 37860 KB Output is correct
93 Correct 654 ms 37604 KB Output is correct
94 Correct 671 ms 36836 KB Output is correct
95 Correct 680 ms 38848 KB Output is correct
96 Correct 662 ms 37092 KB Output is correct
97 Correct 667 ms 39780 KB Output is correct