답안 #927606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927606 2024-02-15T07:08:47 Z guagua0407 Mergers (JOI19_mergers) C++17
0 / 100
50 ms 49100 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=5e5+5;
vector<int> adj[mxn];
int c[mxn];
bool ok[mxn];
map<int,int> mp[mxn];
int cnt[mxn];
int sum[mxn];
int deg[mxn];
int sumok=0;
int ans=0;

void dfs(int v,int p=0){
    mp[v][c[v]]++;
    if(mp[v][c[v]]==cnt[c[v]]) mp[v].erase(c[v]);
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs(u,v);
        if(mp[v].size()<mp[u].size()) swap(mp[v],mp[u]);
        for(auto x:mp[u]){
            mp[v][x.f]+=x.s;
            if(mp[v][x.s]==cnt[x.s]) mp[v].erase(x.s);
        }
        mp[u].clear();
    }
    if(mp[v].empty() and v!=1){
        ok[v]=true;
        ok[p]=true;
    }
}

void dfs2(int v,int p=0){
    if(ok[v]) sum[v]++;
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs2(u,v);
        if(sum[u]>0) deg[v]++;
        sum[v]+=sum[u];
    }
    if(sum[v]!=sumok){
        deg[v]++;
    }
    if(ok[v] and deg[v]<2) ans++;
}

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];
        cnt[c[i]]++;
    }
    dfs(1);
    for(int i=1;i<=n;i++){
        sumok+=ok[i];
    }
    dfs2(1);
    cout<<(ans+1)/2<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

mergers.cpp: In function 'void setIO(std::string)':
mergers.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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.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 9 ms 44076 KB Output is correct
2 Incorrect 9 ms 41564 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 44076 KB Output is correct
2 Incorrect 9 ms 41564 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 44076 KB Output is correct
2 Incorrect 9 ms 41564 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 46732 KB Output is correct
2 Correct 45 ms 49100 KB Output is correct
3 Incorrect 10 ms 41816 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 44076 KB Output is correct
2 Incorrect 9 ms 41564 KB Output isn't correct
3 Halted 0 ms 0 KB -