답안 #154939

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
154939 2019-09-25T15:07:01 Z Mercenary Mergers (JOI19_mergers) C++14
0 / 100
160 ms 47092 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 = 5e5 + 5;

int n , k , col[maxn];
int total[maxn];
int ans = 0;
vector<int> adj[maxn];
int cnt[maxn];
gp_hash_table<int,int> *s[maxn];
int res[maxn];

int cut[maxn];
int sub[maxn];

void DFS(int u , int par){
    int big = -1;
    sub[u] = 1;
    int have = 0;
    for(int c: adj[u]){
        if(c != par){
            DFS(c , u);
            sub[u] += sub[c];
            have |= cut[c];
            if(big == -1 || sub[big] < sub[c])big = c;
        }
    }
    if(u == 1)return;
    if(big == -1){
        s[u] = new gp_hash_table<int,int>();
    }else{
        s[u] = s[big];
        res[u] = res[big];
    }
    auto add = [&](int col , int num){
        (*s[u])[col] += num;
        res[u] += (*s[u])[col] == total[col];
    };
    add(col[u] , 1);
    for(int c : adj[u]){
        if(c != par && c != big){
            for(auto d : (*s[c])){
                add(d.first , d.second);
            }
        }
    }
    cut[u] = (s[u]->size() == res[u]);
    if(have == 0 && cut[u])ans++;
//    cout << u << " " << have << " " << res[u] << endl;
    cut[u] |= have;
}

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 >> col[i];
        total[col[i]]++;
    }
    DFS(1 , 0);
//    cout << ans;
    cout << (ans + 1) / 2;
}

Compilation message

mergers.cpp: In function 'void DFS(int, int)':
mergers.cpp:61:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     cut[u] = (s[u]->size() == res[u]);
               ~~~~~~~~~~~~~^~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:72:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".INP", "r",stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:73:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".OUT", "w",stdout);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12280 KB Output is correct
2 Correct 14 ms 12152 KB Output is correct
3 Correct 14 ms 12124 KB Output is correct
4 Correct 14 ms 12216 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 14 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12152 KB Output is correct
14 Correct 13 ms 12156 KB Output is correct
15 Correct 16 ms 12152 KB Output is correct
16 Incorrect 14 ms 12156 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12280 KB Output is correct
2 Correct 14 ms 12152 KB Output is correct
3 Correct 14 ms 12124 KB Output is correct
4 Correct 14 ms 12216 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 14 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12152 KB Output is correct
14 Correct 13 ms 12156 KB Output is correct
15 Correct 16 ms 12152 KB Output is correct
16 Incorrect 14 ms 12156 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12280 KB Output is correct
2 Correct 14 ms 12152 KB Output is correct
3 Correct 14 ms 12124 KB Output is correct
4 Correct 14 ms 12216 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 14 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12152 KB Output is correct
14 Correct 13 ms 12156 KB Output is correct
15 Correct 16 ms 12152 KB Output is correct
16 Incorrect 14 ms 12156 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 41356 KB Output is correct
2 Incorrect 160 ms 47092 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12280 KB Output is correct
2 Correct 14 ms 12152 KB Output is correct
3 Correct 14 ms 12124 KB Output is correct
4 Correct 14 ms 12216 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 14 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 13 ms 12152 KB Output is correct
14 Correct 13 ms 12156 KB Output is correct
15 Correct 16 ms 12152 KB Output is correct
16 Incorrect 14 ms 12156 KB Output isn't correct
17 Halted 0 ms 0 KB -