답안 #202272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202272 2020-02-15T03:08:53 Z Rakhmand Mergers (JOI19_mergers) C++14
0 / 100
3000 ms 19696 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], used[MXN], city[MXN];
bool check[MXN];
vector<int> g[MXN];
vector<int> state[MXN];
queue<int> q;

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 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++){
        cin >> city[i];
        state[city[i]].pb(i);
    }
    int ans = 0;
    for(int st = 1; st <= k; st++){
        if(check[st] == true){
            continue;
        }
        q.push(st);
        check[st] = 1;
        while(!q.empty()){
            int comp = q.front();
            q.pop();
            int batya;
            for(int i = 0; i < state[comp].size(); i++){
                int x = state[comp][i];
                if(i == 0) batya = x;
                else batya = lca(batya, x);
            }
            bool was = 1;
            for(int i = 0; i < state[comp].size(); i++){
                int x = state[comp][i];
                if(used[x] != 0) was = 0;
                while(x != batya){
                    if(used[x] == 0){
                        if(check[city[x]] == 0) {
                            q.push(city[x]);
                            check[city[x]] = 1;
                        }
                        used[x] = batya;
                        x = dp[x][0];
                    }else{
                        x = used[x];
                        was = 0;
                    }
                }
            }
            ans += (was == 0 ? 0 : 1);
        }
    }
    cout << ans / 2;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:73:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[x].size(); i++){
                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:126:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int i = 0; i < state[comp].size(); i++){
                            ~~^~~~~~~~~~~~~~~~~~~~
mergers.cpp:132:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int i = 0; i < state[comp].size(); i++){
                            ~~^~~~~~~~~~~~~~~~~~~~
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 5116 KB Output is correct
2 Execution timed out 3082 ms 5112 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5116 KB Output is correct
2 Execution timed out 3082 ms 5112 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5116 KB Output is correct
2 Execution timed out 3082 ms 5112 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 95 ms 19696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5116 KB Output is correct
2 Execution timed out 3082 ms 5112 KB Time limit exceeded
3 Halted 0 ms 0 KB -