답안 #406359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
406359 2021-05-17T12:56:50 Z mat_v Inside information (BOI21_servers) C++14
2.5 / 100
602 ms 49552 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define N 120005

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

struct upit{
    int idx;
    int x,y;
};
vector<upit> v;
int n,k;

vector<pii> graf[N];
map<pii,int> edge;

int br = 1;
int cale[N][20];
int good[N];
int suma[N][20];

int disc[N];
int out[N];


void dfs(int x, int y){
    disc[x] = br;
    cale[x][0] = y;
    if(edge[{y,cale[y][0]}] > edge[{x,y}])good[x] = 1;
    suma[x][0] = good[x];
    ff(j,1,17){
        cale[x][j] = cale[cale[x][j - 1]][j - 1];
        suma[x][j] = suma[x][j - 1] + suma[cale[x][j - 1]][j - 1];
    }
    for(auto c:graf[x]){
        if(c.xx == y)continue;
        br++;
        dfs(c.xx, x);
    }
    br++;
    out[x] = br;
}

bool insubtree(int x, int y){
    if(y == 0)return 1;
    if(x == 0)return 0;
    if(disc[x] >= disc[y] && disc[x] < out[y])return 1;
    return 0;
}
int lca(int x, int y){
    if(insubtree(y,x))return x;
    fb(j,17,0)if(!insubtree(y,cale[x][j]))x = cale[x][j];
    return cale[x][0];
}
int dist(int x, int y){
    if(x == y)return 0;
    int ans = 0;
    fb(j,17,0){
        if(!insubtree(y,cale[x][j])){
            x = cale[x][j];
            ans += (1<<j);
        }
    }
    ans++;
    return ans;
}
int koji(int x, int duz){
    if(duz == 0)return x;
    fb(j,17,0)if((1<<j)&duz)x = cale[x][j];
    return x;
}
int sumado(int x, int duz){
    int ans = 0;
    fb(j,17,0){if((1<<j)&duz){x = cale[x][j]; ans += suma[x][j];}}
    ans += suma[x][0];
    return ans;
}




int dsu[N];
int sajz[N];
int findpar(int x){
    if(x == dsu[x])return x;
    return dsu[x] = findpar(dsu[x]);
}
void unite(int a, int b){
    a = findpar(a);
    b = findpar(b);
    if(a == b)return;
    if(sajz[a] < sajz[b])swap(a,b);
    dsu[a] = b;
    sajz[b] += sajz[a];
}



int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> n >> k;
    int dsd = 1;
    ff(i,1,n + k - 1){
        char t;
        cin >> t;
        int x,y;
        cin >> x >> y;
        if(t == 'S'){
            graf[x].pb({y,dsd});
            graf[y].pb({x,dsd});
            edge[{x,y}] = edge[{y,x}] = dsd;
            dsd++;
            v.pb({0,x,y});
        }
        else v.pb({1,x,y});
    }
    dfs(1,0);
    ff(i,1,n){dsu[i] = i; sajz[i] = 1;}
    for(auto c:v){
        if(c.idx == 0){
            unite(c.x, c.y);
            continue;
        }
        if(findpar(c.x) != findpar(c.y)){
            cout << "no\n";
            continue;
        }
        int brat = lca(c.x, c.y);
        int l1 = dist(c.x, brat);
        int l2 = dist(c.y, brat);
        int e1 = 1e9;
        int e2 = -1e9;
        bool d1 = 1;
        bool d2 = 1;
        //cout << l1 << " " << l2 << " " << c.x << " " << c.y << " " << brat << "\n";
        if(l1 >= 1)e1 = edge[{brat,koji(c.x,l1-1)}];
        if(l2 >= 1)e2 = edge[{brat,koji(c.y,l2-1)}];
        //cout << e1 << " " << e2 << "\n";
        if(l1 >= 2){
           // cout << sumado(c.x, l1 - 2) << "\n";
            if(sumado(c.x,l1-2) != 0)d1 = 0;
        }
        if(l2 >= 2){
            if(sumado(c.y,l2-2) != l2 - 1)d2 = 0;
        }
        if(d1 == d2 && e1 > e2 && d1 == 1){
            cout << "yes\n";
            continue;
        }
        else cout << "no\n";
    }

    return 0;
}
/*
6 1
S 1 2
S 1 3
S 3 4
S 4 5
S 1 6
Q 5 1
*/

Compilation message

servers.cpp: In function 'void dfs(int, int)':
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
servers.cpp:46:5: note: in expansion of macro 'ff'
   46 |     ff(j,1,17){
      |     ^~
servers.cpp: In function 'int lca(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
servers.cpp:67:5: note: in expansion of macro 'fb'
   67 |     fb(j,17,0)if(!insubtree(y,cale[x][j]))x = cale[x][j];
      |     ^~
servers.cpp: In function 'int dist(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
servers.cpp:73:5: note: in expansion of macro 'fb'
   73 |     fb(j,17,0){
      |     ^~
servers.cpp: In function 'int koji(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
servers.cpp:84:5: note: in expansion of macro 'fb'
   84 |     fb(j,17,0)if((1<<j)&duz)x = cale[x][j];
      |     ^~
servers.cpp: In function 'int sumado(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
servers.cpp:89:5: note: in expansion of macro 'fb'
   89 |     fb(j,17,0){if((1<<j)&duz){x = cale[x][j]; ans += suma[x][j];}}
      |     ^~
servers.cpp: In function 'int main()':
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
servers.cpp:121:5: note: in expansion of macro 'ff'
  121 |     ff(i,1,n + k - 1){
      |     ^~
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
servers.cpp:136:5: note: in expansion of macro 'ff'
  136 |     ff(i,1,n){dsu[i] = i; sajz[i] = 1;}
      |     ^~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 5316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 5316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 5308 KB Output is correct
2 Correct 602 ms 49540 KB Output is correct
3 Correct 599 ms 49552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 5308 KB Output is correct
2 Correct 602 ms 49540 KB Output is correct
3 Correct 599 ms 49552 KB Output is correct
4 Incorrect 12 ms 5104 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 36 ms 5176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 36 ms 5176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -