답안 #105450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
105450 2019-04-12T11:53:57 Z reality Mergers (JOI19_mergers) C++17
0 / 100
239 ms 113004 KB
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define vii vector < pii >
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;}
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#define pp pair < pair < int , vi > , map < int , int > >
const int N = 1e6 + 5;
int cnt[N];
void comb(pp &a,pp & b) {
    if (a.se.size() < b.se.size())
        swap(a,b);
    if (a.fi.se.size() < b.fi.se.size())
        swap(a.fi.se,b.fi.se);
    for (auto it : b.fi.se)
        a.fi.se.pb(it);
    a.fi.fi += b.fi.fi;
    for (auto it : b.se) {
        a.se[it.fi] += it.se;
        a.fi.fi += a.se[it.fi] == cnt[it.fi] && it.se < cnt[it.fi];
    }
    b.fi.fi = 0;
    b.fi.se.clear();
    b.se.clear();
}
vi g[N];
int v[N];
int flag[N];
int deg[N];
pp M[N];
int R = 0;
void dfs(int node,int prev) {
    M[node].fi.fi = cnt[v[node]] == 1;
    M[node].fi.se.pb(node);
    M[node].se[v[node]] = 1;
    for (auto it : g[node]) {
        if (it == prev)
            continue;
        dfs(it,node);
        comb(M[node],M[it]);
    }
    if (M[node].fi.fi == M[node].se.size()) {
        ++R;
        for (auto it : M[node].fi.se)
            flag[it] = R;
        M[node].fi.fi = 0;
        M[node].fi.se.clear();
        M[node].se.clear();
    }
}
int main(void) {
    int n,k;
    cin.tie(0);
    ios_base :: sync_with_stdio(0);
    cin>>n>>k;
    vii e;
    for (int i = 1;i < n;++i) {
        int a,b;
        cin>>a>>b;
        e.pb(mp(a,b));
        g[a].pb(b);
        g[b].pb(a);
    }
    for (int i = 1;i <= n;++i) {
        cin>>v[i];
        cnt[v[i]] += 1;
    }
    dfs(1,0);
    int ans = 0;
    for (auto it : e)
        if (flag[it.fi] != flag[it.se])
            ++deg[flag[it.fi]],++deg[flag[it.se]];
    for (int i = 1;i <= R;++i)
        ans += deg[i] == 1;
    --ans;
    smax(ans,0);
    cout << ans << '\n';
    return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (M[node].fi.fi == M[node].se.size()) {
         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 102136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 102136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 102136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 112204 KB Output is correct
2 Incorrect 239 ms 113004 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 102136 KB Output isn't correct
2 Halted 0 ms 0 KB -