답안 #378117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
378117 2021-03-16T03:33:36 Z syl123456 Mergers (JOI19_mergers) C++17
0 / 100
104 ms 9316 KB
#include <bits/stdc++.h>
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const ll mod = 1e9 + 7;
vector<vector<int>> g;
vector<int> rt, low;
vector<bool> vis;
void dfs(int i, int p, int d) {
    vis[i] = true, low[i] = d;
    for (int j : g[i]) if (j != p) {
        if (!vis[j]) dfs(j, i, d + 1);
        low[i] = min(low[i], low[j]);
    }
    rt[i] = low[i] < d ? p : i;
}
int find(int i) {
    return rt[i] = rt[i] == i ? i : find(rt[i]);
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;
    g.resize(n), rt.resize(n), low.resize(n), vis.assign(n, false);
    for (int i = 0; i < n - 1; ++i) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        g[x].push_back(y), g[y].push_back(x);
    }
    vector<int> last(k, -1);
    for (int i = 0, x; i < n; ++i) {
        cin >> x, --x;
        if (~last[x])
            g[i].push_back(last[x]), g[last[x]].push_back(i);
        last[x] = i;
    }
    dfs(0, 0, 0);
    for (int i = 0; i < n; ++i) find(i);
    for (int i = 0; i < n; ++i) {
        for (int &j : g[i]) j = rt[j];
        if (rt[i] != i) for (int &j : g[i]) g[rt[i]].push_back(j);
    }
    int lf = 0;
    for (int i = 0; i < n; ++i) if (rt[i] == i) {
        bool b = true;
        for (int j : g[i]) if (j != g[i][0]) b = false;
        if (b) ++lf;
    }
    if (lf == 1) return cout << 0, 0;
    cout << (lf + 1 >> 1);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:53:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |     cout << (lf + 1 >> 1);
      |              ~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 9316 KB Output is correct
2 Correct 64 ms 7396 KB Output is correct
3 Incorrect 2 ms 492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -