답안 #546308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546308 2022-04-07T07:54:46 Z Jarif_Rahman Jail (JOI22_jail) C++17
0 / 100
14 ms 340 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, m;
vector<vector<int>> v;
vector<int> s, t;
vector<vector<int>> sth;
vector<vector<int>> sth2;
vector<int> state;
vector<int> p;
vector<int> d;
bool cycle;

void dfs1(int nd, int ss, int dis){
    for(int x: v[nd]) if(x != ss) dfs1(x, nd, dis+1);
    p[nd] = ss;
    d[nd] = dis;
}

void dfs2(int nd){
    if(state[nd] == 1){
        cycle = 1;
        return;
    }
    if(state[nd] == 2) return;
    state[nd] = 1;
    for(int x: sth[nd]) dfs2(x);
    state[nd] = 2;
}

void solve(){
    cin >> n;
    v.assign(n, {});
    p.assign(n, 0);
    d.assign(n, 0);
    sth.assign(n, {});
    sth2.assign(n, {});
    state.assign(n, 0);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);
    }

    dfs1(0, -1, 0);

    cin >> m;
    s.assign(m, 0);
    t.assign(m, 0);
    for(int i = 0; i < m; i++) cin >> s[i] >> t[i], s[i]--, t[i]--;

    vector<int> ss(n, -1), tt(n, -1);
    for(int i = 0; i < m; i++) ss[s[i]] = i, tt[t[i]] = i;

    auto get_path = [&](int a, int b){
        vector<int> rt;
        if(d[a] > d[b]) swap(a, b);
        while(d[a] != d[b]) rt.pb(b), b = p[b];
        while(a != b) rt.pb(a), rt.pb(b), a = p[a], b = p[b];
        rt.pb(a);
        return rt;
    };

    for(int i = 0; i < m; i++){
        unordered_map<int, int> cnt;
        for(int x: get_path(s[i], t[i])){
            if(ss[x] != -1 && ss[x] != i) cnt[ss[x]]++, sth[i].pb(ss[x]);
            //if(tt[x] != -1 && tt[x] != i) cnt[tt[x]]++, sth[tt[x]].pb(i);
        }
        for(auto [ig, x]: cnt) if(x > 1){
            cout << "No\n";
            return;
        }
    }

    cycle = 0;

    for(int i = 0; i < n; i++) if(state[i] == 0) dfs2(i);

    for(int i = 0; i < m; i++){
        unordered_map<int, int> cnt;
        for(int x: get_path(s[i], t[i])){
            if(ss[x] != -1 && ss[x] != i) cnt[ss[x]]++, sth[i].pb(ss[x]);
            //if(tt[x] != -1 && tt[x] != i) cnt[tt[x]]++, sth[tt[x]].pb(i);
        }
        for(auto [ig, x]: cnt) if(x > 1){
            cout << "No\n";
            return;
        }
    }

    for(int i = 0; i < n; i++) if(state[i] == 0) dfs2(i);

    cout << (cycle?"No\n":"Yes\n");
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T; cin >> T; while(T--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 10 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 232 KB Output is correct
5 Incorrect 14 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 10 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -