Submission #136544

# Submission time Handle Problem Language Result Execution time Memory
136544 2019-07-25T13:27:27 Z egorlifar Synchronization (JOI13_synchronization) C++17
20 / 100
493 ms 65016 KB
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
 
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 100228;


int n, m, q;
int a[MAXN], b[MAXN];
bool on[MAXN];
int ans[MAXN];
vector<pair<int, int> > g[MAXN];
vector<pair<int, int> > pos[MAXN];
map<pair<int, int>, int> who;
map<pair<int, int>, bool> was;
map<pair<int, int>, vector<int> > cnt;
pair<int, int> f[MAXN];
int last[MAXN];


void calc(int a, int b) {
    if (was[{a, b}]) {
        return;
    }
    int id = who[{a, b}];
    was[{a, b}] = true;
    if (pos[id].empty()) {
        return;
    }
    cnt[{a, b}].resize(sz(pos[id]));
    set<pair<int, pair<int, int> > > s;
    vector<int> fk(sz(pos[id]), 0);
    for (auto h: g[b]) {
        if (h.first != a) {
           // cout << b + 1 << ' ' << h.first + 1 << endl; 
            calc(b, h.first);
            int id1 = h.second;
            if (!pos[id1].empty()) {
                int it = 0;
                int last = 0;
                for (auto x: pos[id1]) {
                    int l = 0;
                    int r = sz(pos[id]) - 1;
                    while (l < r) {
                        int mid = (l + r) >> 1;
                        if (pos[id][mid].second >= x.first)  {
                            r = mid;
                        } else {
                            l = mid + 1;
                        }
                    }
                    if (pos[id][l].second >= x.first) {
                        // cout << pos[id][l].first << ' ' << x.second << endl;
                        // if (pos[id][l].first <= x.second) {
                            fk[l] += cnt[{b, h.first}][it] - last;
                            last = cnt[{b, h.first}][it];
                      //  }
                    }
                    it++;
                }
            }
        }
    }
    int cntf = 1;
    for (int i = 0; i < sz(pos[id]); i++) { 
        cntf += fk[i];
        cnt[{a, b}][i] = cntf;
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //read(FILENAME); 
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        g[a[i]].pb({b[i], i});
        g[b[i]].pb({a[i], i});
        who[{a[i], b[i]}] = i;
        who[{b[i], a[i]}] = i;
    }   
    for (int i = 0; i < m; i++) {
        int id;
        cin >> id;
        id--;
        if (on[id]) {
            on[id] = false;
            pos[id].pb({last[id], i - 1});
        } else {
            on[id] = true;
            last[id] = i;
        }
    }
    for (int i = 0; i < n - 1; i++) {
        if (on[i]) {
            pos[i].pb({last[i], m - 1});
        }
    }
    for (int i = 0; i < q; i++) {
        int c;
        cin >> c;
        c--;
        int res = 1;
        int kek = 0;
        for (auto h: g[c]) {
            if (!pos[h.second].empty()) {
                chkmax(kek, pos[h.second].back().second);
            }
        }
        for (auto h: g[c]) {
            if (!pos[h.second].empty()) {
                if (kek == pos[h.second].back().second) {
                    calc(c, h.first);
                    calc(h.first, c);
                    //cout << c + 1 << ' ' << h.first + 1 << ' ' << cnt[{c, h.first}].back() << ' ' << cnt[{h.first, c}].back() << endl;
                    res += cnt[{c, h.first}].back() + cnt[{h.first, c}].back() - 1;
                    break;  
                }
            }
        }
        cout << res << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5244 KB Output is correct
5 Incorrect 6 ms 5112 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 308 ms 55252 KB Output is correct
2 Correct 296 ms 49780 KB Output is correct
3 Correct 314 ms 65016 KB Output is correct
4 Correct 305 ms 63864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5112 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 493 ms 56592 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Incorrect 9 ms 5240 KB Output isn't correct
5 Halted 0 ms 0 KB -