답안 #202277

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202277 2020-02-15T04:59:26 Z Rakhmand Mergers (JOI19_mergers) C++14
0 / 100
3000 ms 18800 KB
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for(int i = start; i <= finish; i += k)

const int MXN = 1e5 + 10;
const long long MNN = 1e6 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e16;
const int OO = 1e9 + 10;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

void istxt(bool yes){
    if(yes == 1){
        freopen("balancing.in", "r", stdin);
        freopen("balancing.out", "w", stdout);
    }else{
        freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
    }
}

int n, k, dp[MXN][20], lvl[MXN], city[MXN], cnt[MXN], p[MXN], lol[MXN];
vector<int> g[MXN];
vector<int> state[MXN];

void dfs(int x, int p){
    dp[x][0] = p;
    for(int i = 1; i < 20; i++){
        dp[x][i] = dp[dp[x][i - 1]][i - 1];
    }
    lvl[x] = lvl[p] + 1;
    for(int i = 0; i < g[x].size(); i++){
        int to = g[x][i];
        if(to == p) continue;
        dfs(to, x);
    }
}

int lca(int u, int v){
    if(lvl[u] < lvl[v]){
        swap(u, v);
    }
    int dist = lvl[u] - lvl[v];
    for(int i = 0; i < 20; i++){
        if((1 << i) & dist){
            u = dp[u][i];
        }
    }
    if(u == v) return u;
    for(int i = 19; i >= 0; i--){
        if(dp[u][i] != dp[v][i]){
            u = dp[u][i];
            v = dp[v][i];
        }
    }
    return dp[v][0];
}

int find(int x) { return (x == p[x] ? x : find(p[x])); }
void attach(int u, int v) {u = find(u), v = find(v), p[u] = v; }

void dfs2(int v, int p){
    for(int i = 0; i < g[v].size(); i++){
        int to = g[v][i];
        if(to == p) continue;
        dfs2(to, v);
        cnt[v] += cnt[to];
    }
    if(v != 1 && cnt[v]) attach(p, v);
}

int main () {
    ios;
    //istxt(0);
    cin >> n >> k;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++){
        p[i] = i;
        cin >> city[i];
        state[city[i]].pb(i);
    }
    for(int st = 1; st <= k; st++){
        for(int j = 1; j < state[st].size(); j++){
            int u = state[st][j - 1], v = state[st][j];
            int batya = lca(u, v);
            cnt[u]++, cnt[v]++, cnt[batya] --, cnt[batya]--;
        }
    }
    dfs2(1, 1);
    
    for(int i = 2; i <= n; i++){
        int batya = dp[i][0];
        if(find(batya) == find(i)) continue;
        lol[batya]++;
        lol[i]++;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(lol[i] == 1) ans++;
    }
    ans++;
    cout << ans / 2;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:71:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[x].size(); i++){
                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:102:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[v].size(); i++){
                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:128:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 1; j < state[st].size(); j++){
                        ~~^~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'void istxt(bool)':
mergers.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("balancing.in", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("balancing.out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3056 ms 18800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -