Submission #1343764

#TimeUsernameProblemLanguageResultExecution timeMemory
1343764Jakub_WozniakInside information (BOI21_servers)C++20
48 / 100
3595 ms20828 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define st first
#define nd second
const int maxn = 3*(1e+5)+9;
int t[maxn] , siz[maxn];
int mostG[maxn] , mostD[maxn]; // G rosnie , D maleje
int dep[maxn] , ojc[maxn] , jump[maxn];
int Cojc[maxn];
char Cz[maxn];
int A[maxn] , B[maxn];
vector <pii> T[maxn];

int find(int X)
{
    if(t[X] == X)return X;
    return t[X] = find(t[X]);
}

void union_(int a, int b)
{
    a = find(a) , b = find(b);
    
    if(siz[a] < siz[b])
    {
        t[a] = b;
        siz[b] += siz[a];
    }
    else
    {
        t[b] = a;
        siz[a] += siz[b];
    }
}

int F(int x , int w)
{
    while(w)
    {
        if(dep[x] - dep[jump[x]] <= w)
        {
            w -= (dep[x] - dep[jump[x]]);
            x = jump[x];
        }
        else
        {
            w--;
            x = ojc[x];
        }
    }
    return x;
}

pair<int,bool> LCA(int a,  int b)
{
    if(dep[a] < dep[b]) b = F(b , dep[b]-dep[a]);
    if(dep[a] > dep[b]) a = F(a , dep[a]-dep[b]);
    if(a == b)return {a,1};
    while(ojc[a] != ojc[b])
    {
        if(jump[a] == jump[b])
        {
            a = ojc[a];
            b = ojc[b];
        }
        else
        {
            a = jump[a];
            b = jump[b];
        }
    }
    return {ojc[a] , (Cojc[a] > Cojc[b])};
}

bool sprawdz(int x ,int d)
{
    if(find(x) == find(d))
    {
        auto RLCA = LCA(x,d);
        if(RLCA.nd && dep[mostG[d]] <= dep[RLCA.st] && dep[mostD[x]] <= dep[RLCA.st])return 1;
        return 0;
    }
    else
    {
        return 0;
    } 
}

void DFS(int v ,int o)
{
    ojc[v] = o;
    dep[v] = dep[o]+1;
    if(dep[jump[jump[o]]] - dep[jump[o]] == dep[jump[o]] - dep[o])
    {
        jump[v] = jump[jump[o]];
    }
    else jump[v] = o;

    for(auto p : T[v])
    {
        if(p.st == o)continue;
        Cojc[p.st] = p.nd;
        if(p.nd < Cojc[v])
        {
            mostG[p.st] = mostG[v];
            mostD[p.st] = v;
        }
        else
        {
            mostD[p.st] = mostD[v];
            mostG[p.st] = v;
        }

        DFS(p.st,v);
    }

}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n , K ;
    cin >> n >> K;

    for(int i = 1; i <= n; i++)
    {
        t[i] = i;
        siz[i] = 1;
        mostD[i] = i;
        mostG[i] = i;
    }

    int Q = n+K-1;
    for(int i = 0 ; i < Q ; i++)
    {
        cin >> Cz[i];
        if(Cz[i] == 'C')cin >> A[i];
        else cin >> A[i] >> B[i];

        if(Cz[i] == 'S')
        {
            T[A[i]].push_back({B[i],i});
            T[B[i]].push_back({A[i],i});
        }
    }

    DFS(1,0);



    char C;
    int a , b;
    for(int i = 0 ; i < Q ; i++)
    {
        C = Cz[i];
        if(C == 'C')
        {
            a = A[i];
            int ile =0 ;
            for(int i = 1; i <= n ; i++) if(sprawdz(i,a))ile++;
            cout << ile << '\n';
        }
        else if(C == 'Q')
        {
            a = A[i] , b = B[i];
            if(sprawdz(a,b))cout << "yes\n";
            else cout << "no\n"; 
        }
        else
        {
            a = A[i] , b = B[i];
            if(dep[a] > dep[b])swap(a,b);
            union_(a,b);            
        }
    }


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...