#include <vector>
#include "incursion.h"
#include <cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
using lint = long long;
using intp = pair<int,int>;
using vint = vector<int>;
using vintp = vector<intp>;
const int MAX_N=45010;
int n;
vintp eg[MAX_N];
int sz[MAX_N];
vint val;
int isleaf=0;
bool compe(const intp &e1,const intp &e2)
{
return e1.second>e2.second;
}
int fillsz(int v,int p)
{
sz[v]=1;
for(auto &e : eg[v])
if(e.first!=p)
{
e.second=fillsz(e.first,v);
sz[v]+=e.second;
}
for(auto &e : eg[v])
if(e.first==p)
e.second=n-sz[v];
sort(eg[v].begin(),eg[v].end(),compe);
return sz[v];
}
bool ismid(int v)
{
int mx=0;
for(auto e : eg[v])
mx=max(mx,e.second);
int c=0;
for(auto e : eg[v])
if(e.second==mx)c++;
return c>=2;
}
void dfs(int v,int p)
{
if(ismid(v))
{
val[v-1]=isleaf;
}
else if(p==0)
{
val[v-1]=0;
}
else
{
int mx=0;
for(auto e : eg[v])
mx=max(mx,e.second);
for(auto e : eg[v])
if(e.first==p)
val[v-1]=(e.second==mx);
}
for(auto e : eg[v])
if(e.first!=p)
dfs(e.first,v);
}
vint mark(vintp F, int safe)
{
n=F.size()+1;
for(int i=1;i<=n;i++)
eg[i].clear();
for(int i=0;i<n-1;i++)
{
int u=F[i].first,v=F[i].second;
eg[u].push_back({v,0});
eg[v].push_back({u,0});
}
val.resize(n);
fillsz(safe,0);
isleaf=(eg[safe].size()<=1);
dfs(safe,0);
return val;
}
bool checkleaf(int v,int p)
{
if(isleaf==-1)return false;
int flag=1;
for(auto e : eg[v])
if(e.first!=p && eg[e.first].size()>1)flag=0;
if(flag==0)return false;
if(isleaf==0)return true;
for(auto e : eg[v])
if(e.first!=p)
{
if(visit(e.first)==0)return true;
visit(v);
}
return false;
}
void calc(int v,int p,int t)
{
if(ismid(v))
{
isleaf=t;
if(checkleaf(v,p))
return;
for(auto e : eg[v])
if(e.first!=p)
{
int nt=visit(e.first);
if(nt==0)
{
calc(e.first,v,nt);
return;
}
visit(v);
}
return;
}
if(checkleaf(v,p))
return;
if(t==1)return calc(eg[v][0].first,v,visit(eg[v][0].first));
if(t==0 && eg[v].size()<=1)return;
int nt;
if(eg[v][1].first!=p)
{
nt=visit(eg[v][1].first);
if(nt==0)
{
calc(eg[v][1].first,v,nt);
return;
}
visit(v);
}
if(eg[v][2].first!=p)
{
nt=visit(eg[v][2].first);
if(nt==0)
{
calc(eg[v][2].first,v,nt);
return;
}
visit(v);
}
}
void locate(vintp F, int curr, int t)
{
n=F.size()+1;
for(int i=1;i<=n;i++)
eg[i].clear();
for(int i=0;i<n-1;i++)
{
int u=F[i].first,v=F[i].second;
eg[u].push_back({v,0});
eg[v].push_back({u,0});
}
fillsz(curr,0);
isleaf=-1;
calc(curr,0,t);
return;
}
Compilation message
interface.cpp: In function 'int main()':
interface.cpp:44:55: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(fread(T.data(), sizeof(int), 2 * N - 2, stdin) != 2 * N - 2) exit(0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
interface.cpp:50:33: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | int l = (numbers.size() == N ? N : 0);
| ~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2816 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
13032 KB |
Correct |
2 |
Correct |
167 ms |
13280 KB |
Correct |
3 |
Incorrect |
95 ms |
14176 KB |
Not correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
8728 KB |
Correct |
2 |
Correct |
64 ms |
8832 KB |
Correct |
3 |
Correct |
74 ms |
8848 KB |
Correct |
4 |
Correct |
77 ms |
11360 KB |
Correct |
5 |
Correct |
120 ms |
11392 KB |
Correct |
6 |
Correct |
133 ms |
11632 KB |
Correct |
7 |
Correct |
68 ms |
8536 KB |
Correct |
8 |
Correct |
67 ms |
8700 KB |
Correct |
9 |
Correct |
73 ms |
8532 KB |
Correct |
10 |
Correct |
65 ms |
8608 KB |
Correct |
11 |
Correct |
75 ms |
8476 KB |
Correct |
12 |
Correct |
65 ms |
8608 KB |
Correct |
13 |
Correct |
72 ms |
8544 KB |
Correct |
14 |
Correct |
51 ms |
7212 KB |
Correct |
15 |
Correct |
51 ms |
7204 KB |
Correct |
16 |
Correct |
63 ms |
8576 KB |
Correct |
17 |
Correct |
65 ms |
8664 KB |
Correct |
18 |
Correct |
66 ms |
8572 KB |
Correct |
19 |
Correct |
68 ms |
8540 KB |
Correct |
20 |
Correct |
47 ms |
7216 KB |
Correct |
21 |
Correct |
51 ms |
7220 KB |
Correct |
22 |
Correct |
49 ms |
7332 KB |
Correct |
23 |
Correct |
48 ms |
7208 KB |
Correct |
24 |
Correct |
53 ms |
7216 KB |
Correct |
25 |
Correct |
47 ms |
6948 KB |
Correct |
26 |
Correct |
75 ms |
8820 KB |
Correct |
27 |
Correct |
73 ms |
8480 KB |
Correct |
28 |
Correct |
70 ms |
8576 KB |
Correct |
29 |
Correct |
72 ms |
8584 KB |
Correct |
30 |
Correct |
81 ms |
8972 KB |
Correct |
31 |
Correct |
64 ms |
8584 KB |
Correct |
32 |
Correct |
69 ms |
8616 KB |
Correct |
33 |
Correct |
68 ms |
8536 KB |
Correct |
34 |
Correct |
65 ms |
8612 KB |
Correct |
35 |
Correct |
60 ms |
8816 KB |
Correct |
36 |
Correct |
67 ms |
8844 KB |
Correct |
37 |
Correct |
66 ms |
8604 KB |
Correct |
38 |
Correct |
74 ms |
8824 KB |
Correct |
39 |
Correct |
61 ms |
8588 KB |
Correct |
40 |
Correct |
70 ms |
8856 KB |
Correct |
41 |
Correct |
68 ms |
8832 KB |
Correct |
42 |
Correct |
75 ms |
8572 KB |
Correct |
43 |
Correct |
70 ms |
8832 KB |
Correct |
44 |
Correct |
74 ms |
8956 KB |
Correct |
45 |
Correct |
66 ms |
8632 KB |
Correct |
46 |
Correct |
66 ms |
8736 KB |
Correct |
47 |
Correct |
75 ms |
8852 KB |
Correct |
48 |
Correct |
74 ms |
8752 KB |
Correct |
49 |
Correct |
74 ms |
8536 KB |
Correct |
50 |
Correct |
72 ms |
8864 KB |
Correct |
51 |
Correct |
69 ms |
8580 KB |
Correct |
52 |
Correct |
69 ms |
8884 KB |
Correct |
53 |
Correct |
71 ms |
8900 KB |
Correct |
54 |
Correct |
65 ms |
8844 KB |
Correct |
55 |
Correct |
66 ms |
8600 KB |
Correct |
56 |
Correct |
74 ms |
8824 KB |
Correct |
57 |
Correct |
71 ms |
8580 KB |
Correct |
58 |
Correct |
70 ms |
8832 KB |
Correct |
59 |
Incorrect |
56 ms |
8820 KB |
Not correct |
60 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2816 KB |
Correct |
2 |
Correct |
174 ms |
13032 KB |
Correct |
3 |
Correct |
167 ms |
13280 KB |
Correct |
4 |
Incorrect |
95 ms |
14176 KB |
Not correct |
5 |
Halted |
0 ms |
0 KB |
- |