답안 #77754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77754 2018-09-30T08:33:31 Z ansol4328 트리 (KOI16_treeM) C++
43 / 100
169 ms 66560 KB
#include<stdio.h>
#include<memory.h>
#include<vector>

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];
vector<bool> res;
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.push_back(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

tree.cpp: In function 'int main()':
tree.cpp:44: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:45: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:48: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:49: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]);
                         ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1276 KB Output is correct
2 Correct 3 ms 1380 KB Output is correct
3 Correct 4 ms 1528 KB Output is correct
4 Correct 3 ms 1548 KB Output is correct
5 Correct 3 ms 1632 KB Output is correct
6 Correct 4 ms 1784 KB Output is correct
7 Correct 3 ms 1784 KB Output is correct
8 Correct 4 ms 1784 KB Output is correct
9 Correct 4 ms 1784 KB Output is correct
10 Correct 3 ms 1800 KB Output is correct
11 Correct 3 ms 1824 KB Output is correct
12 Correct 3 ms 1824 KB Output is correct
13 Correct 4 ms 1900 KB Output is correct
14 Correct 3 ms 1900 KB Output is correct
15 Correct 3 ms 1964 KB Output is correct
16 Correct 3 ms 1988 KB Output is correct
17 Correct 4 ms 2028 KB Output is correct
18 Correct 3 ms 2052 KB Output is correct
19 Correct 4 ms 2076 KB Output is correct
20 Correct 4 ms 2100 KB Output is correct
21 Correct 4 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1276 KB Output is correct
2 Correct 3 ms 1380 KB Output is correct
3 Correct 4 ms 1528 KB Output is correct
4 Correct 3 ms 1548 KB Output is correct
5 Correct 3 ms 1632 KB Output is correct
6 Correct 4 ms 1784 KB Output is correct
7 Correct 3 ms 1784 KB Output is correct
8 Correct 4 ms 1784 KB Output is correct
9 Correct 4 ms 1784 KB Output is correct
10 Correct 3 ms 1800 KB Output is correct
11 Correct 3 ms 1824 KB Output is correct
12 Correct 3 ms 1824 KB Output is correct
13 Correct 4 ms 1900 KB Output is correct
14 Correct 3 ms 1900 KB Output is correct
15 Correct 3 ms 1964 KB Output is correct
16 Correct 3 ms 1988 KB Output is correct
17 Correct 4 ms 2028 KB Output is correct
18 Correct 3 ms 2052 KB Output is correct
19 Correct 4 ms 2076 KB Output is correct
20 Correct 4 ms 2100 KB Output is correct
21 Correct 4 ms 2124 KB Output is correct
22 Correct 95 ms 7508 KB Output is correct
23 Correct 91 ms 9696 KB Output is correct
24 Correct 83 ms 11944 KB Output is correct
25 Correct 89 ms 14348 KB Output is correct
26 Correct 123 ms 16444 KB Output is correct
27 Correct 77 ms 18684 KB Output is correct
28 Correct 75 ms 20932 KB Output is correct
29 Correct 77 ms 23180 KB Output is correct
30 Correct 79 ms 25484 KB Output is correct
31 Correct 74 ms 27756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 36248 KB Output is correct
2 Correct 164 ms 41744 KB Output is correct
3 Correct 155 ms 47188 KB Output is correct
4 Correct 155 ms 52752 KB Output is correct
5 Correct 160 ms 58372 KB Output is correct
6 Correct 165 ms 63900 KB Output is correct
7 Runtime error 169 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1276 KB Output is correct
2 Correct 3 ms 1380 KB Output is correct
3 Correct 4 ms 1528 KB Output is correct
4 Correct 3 ms 1548 KB Output is correct
5 Correct 3 ms 1632 KB Output is correct
6 Correct 4 ms 1784 KB Output is correct
7 Correct 3 ms 1784 KB Output is correct
8 Correct 4 ms 1784 KB Output is correct
9 Correct 4 ms 1784 KB Output is correct
10 Correct 3 ms 1800 KB Output is correct
11 Correct 3 ms 1824 KB Output is correct
12 Correct 3 ms 1824 KB Output is correct
13 Correct 4 ms 1900 KB Output is correct
14 Correct 3 ms 1900 KB Output is correct
15 Correct 3 ms 1964 KB Output is correct
16 Correct 3 ms 1988 KB Output is correct
17 Correct 4 ms 2028 KB Output is correct
18 Correct 3 ms 2052 KB Output is correct
19 Correct 4 ms 2076 KB Output is correct
20 Correct 4 ms 2100 KB Output is correct
21 Correct 4 ms 2124 KB Output is correct
22 Correct 95 ms 7508 KB Output is correct
23 Correct 91 ms 9696 KB Output is correct
24 Correct 83 ms 11944 KB Output is correct
25 Correct 89 ms 14348 KB Output is correct
26 Correct 123 ms 16444 KB Output is correct
27 Correct 77 ms 18684 KB Output is correct
28 Correct 75 ms 20932 KB Output is correct
29 Correct 77 ms 23180 KB Output is correct
30 Correct 79 ms 25484 KB Output is correct
31 Correct 74 ms 27756 KB Output is correct
32 Correct 159 ms 36248 KB Output is correct
33 Correct 164 ms 41744 KB Output is correct
34 Correct 155 ms 47188 KB Output is correct
35 Correct 155 ms 52752 KB Output is correct
36 Correct 160 ms 58372 KB Output is correct
37 Correct 165 ms 63900 KB Output is correct
38 Runtime error 169 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
39 Halted 0 ms 0 KB -