Submission #77756

#TimeUsernameProblemLanguageResultExecution timeMemory
77756ansol4328트리 (KOI16_treeM)C++98
71 / 100
206 ms66560 KiB
#include<stdio.h>
#include<memory.h>

using namespace std;

struct uf
{
    int p[200002];
    uf()
    {
        memset(p,-1,sizeof(p));
    }
    int f(int v)
    {
        if(p[v]==-1) return v;
        p[v]=f(p[v]);
        return p[v];
    }
    bool same(int a, int b)
    {
        a=f(a);
        b=f(b);
        if(a==b) return true;
        return false;
    }
    void u(int a, int b)
    {
        a=f(a);
        b=f(b);
        if(a==b) return;
        p[a]=b;
    }
};

int n, q;
int p[200002];
int query[400002][3], cnt;
bool res[200002];
uf T;

int main()
{
    scanf("%d %d",&n,&q);
    for(int i=2 ; i<=n ; i++) scanf("%d",&p[i]);
    for(int i=1 ; i<=n-1+q ; i++)
    {
        scanf("%d %d",&query[i][0],&query[i][1]);
        if(query[i][0]) scanf("%d",&query[i][2]);
    }
    for(int i=n-1+q ; i>=1 ; i--)
    {
        if(query[i][0]==0) T.u(p[query[i][1]],query[i][1]);
        else res[cnt++]=T.same(query[i][1],query[i][2]);
    }
    for(int i=q-1 ; i>=0 ; i--) printf("%s\n",res[i] ? "YES" : "NO");
    return 0;
}

Compilation message (stderr)

tree.cpp: In function 'int main()':
tree.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&q);
     ~~~~~^~~~~~~~~~~~~~~
tree.cpp:44:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=2 ; i<=n ; i++) scanf("%d",&p[i]);
                               ~~~~~^~~~~~~~~~~~
tree.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&query[i][0],&query[i][1]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tree.cpp:48:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         if(query[i][0]) scanf("%d",&query[i][2]);
                         ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...