답안 #546940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546940 2022-04-09T03:35:11 Z ngrace Jail (JOI22_jail) C++14
0 / 100
41 ms 340 KB
#include <vector>
#include <iostream>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
#define v vector
#define pii pair<int,int>
#define fi first
#define se second

int N,M;
int anc=20;
v<v<int>> adj;
v<int> depth;
v<v<int>> par;
void dfs(int cur, int from){
    if(depth[cur]!=-1) return;
    depth[cur]=depth[from]+1;
    par[cur][0]=from;
    for(int it:adj[cur]) dfs(it, cur);
}
bool is_anc(int cur, int other){
    for(int i=anc-1; i>=0; i--){
        if(depth[par[cur][i]]>=depth[other]) cur=par[cur][i];
    }
    if(cur==other) return true;
    return false;
}

int lca(int a, int b){
    if(depth[a]<depth[b]) swap(a,b);
    for(int i=anc-1; i>=0; i--){
        if(depth[par[a][i]]>=depth[b]) a=par[a][i];
    }
    if(a==b) return a;
    for(int i=anc-1; i>=0; i--){
        if(par[a][i]!=par[b][i]){
            a=par[a][i];
            b=par[b][i];
        }
    }
    return par[a][0];
}
bool path(int a, int b, int c){//does c lie on the path of a and b
    int l=lca(a,b);
    if(is_anc(c,l) && (is_anc(a,c)||is_anc(b,c))) return true;
    return false;
}

v<bool> prevrec;
v<bool> vis;
bool find_cycle(int cur){
    if(!vis[cur]){
        vis[cur]=true;
        prevrec[cur]=true;
        for(int it:adj[cur]){
            if(!vis[it] && find_cycle(it)){
                return true;
            }
            else if(prevrec[it]){
                return true;
            }
        }
    }
    prevrec[cur]=false;
    return false;
}

int main(){
    int Q;
    cin>>Q;
    for(int q=0;q<Q;q++){
        cin>>N;
        adj=v<v<int>>(N);
        for(int i=0; i<N-1; i++){
            int a,b;
            cin>>a>>b;
            a--;
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        depth=v<int>(N,-1);
        par=v<v<int>>(N,v<int>(anc,-1));
        dfs(0,0);
        for(int i=1; i<anc; i++){
            for(int j=0; j<N; j++){
                par[j][i]=par[par[j][i-1]][i-1];
            }
        }

        cin>>M;
        v<pii> rooms(M);
        for(int i=0; i<M; i++){
            cin>>rooms[i].fi>>rooms[i].se;
            rooms[i].fi--;
            rooms[i].se--;
        }

        adj = v<v<int>>(M);
        for(int i=0; i<M; i++){
            for(int j=0; j<M; j++){
                if(i==j) break;
                bool sj=path(rooms[i].fi, rooms[i].se, rooms[j].fi);
                bool ej=path(rooms[i].fi, rooms[i].se, rooms[j].se);

                if(sj) adj[j].push_back(i);
                if(ej) adj[i].push_back(j);
            }
        }


        bool cycle=false;
        vis = v<bool>(M,false);
        prevrec = v<bool>(M,false);
        for(int i=0; i<M; i++){
            if(!vis[i] && find_cycle(i)){
                cycle=true;
                break;
            }
        }

        if(cycle) cout<<"No\n";
        else cout<<"Yes\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 33 ms 212 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 3 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 3 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 3 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 3 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 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 41 ms 296 KB Output isn't correct
6 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 Correct 0 ms 212 KB Output is correct
4 Incorrect 33 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -