QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#115588#4926. Where Is the Root?jamielim0 6ms3712kbC++143.4kb2023-06-26 12:25:082023-06-26 12:25:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-26 12:25:10]
  • 评测
  • 测评结果:0
  • 用时:6ms
  • 内存:3712kb
  • [2023-06-26 12:25:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
typedef pair<int,int> ii;
typedef long long ll;
typedef unsigned long long ull;
const int INF=1012345678;
const ll LLINF=1012345678012345678LL;
const ll MOD=1000000007;

int n;
vector<int> adj[505];
int dist[2][505];
int fr;
int dep[505];
int maxdep;
vector<int> leaf;
bool lf[505];

void findFakeRoot(){
	for(int i=1;i<=n;i++)dist[0][i]=dist[1][i]=INF;
	dist[0][1]=0;
	queue<int> q;
	q.push(1);
	int maxi=1;
	while(!q.empty()){
		int cur=q.front();q.pop();
		for(int i:adj[cur]){
			if(dist[0][i]>dist[0][cur]+1){
				dist[0][i]=dist[0][cur]+1;
				q.push(i);
				if(dist[0][i]>dist[0][maxi])maxi=i;
			}
		}
	}
	dist[1][maxi]=0;
	q.push(maxi);
	while(!q.empty()){
		int cur=q.front();q.pop();
		for(int i:adj[cur]){
			if(dist[1][i]>dist[1][cur]+1){
				dist[1][i]=dist[1][cur]+1;
				q.push(i);
				if(dist[1][i]>dist[1][maxi])maxi=i;
			}
		}
	}
	fr=maxi;
	while(dist[1][fr]>dist[1][maxi]/2){
		for(int i:adj[fr]){
			if(dist[1][i]==dist[1][fr]-1){
				fr=i;break;
			}
		}
	}
}

void dfs(int x,int p){
	maxdep=max(maxdep,dep[x]);
	int c=0;
	for(int i:adj[x]){
		if(i==p)continue;
		dep[i]=dep[x]+1;
		dfs(i,x);
		c++;
	}
	if(c==0){
		leaf.pb(x);
		lf[x]=1;
	}
}

vector<int> t;
void dfs2(int x,int p,int avoid){
	int c=0;
	for(int i:adj[x]){
		if(i==p)continue;
		if(i==avoid)continue;
		dfs2(i,x,avoid);
		c++;
	}
	if(c==0)t.pb(x);
}
vector<int> getOtherLeaves(int x){
	t.clear();
	dfs2(fr,0,x);
	return t;
}

bool query(vector<int> v){
	sort(ALL(v));
	v.resize(unique(ALL(v))-v.begin());
	printf("? %d",SZ(v));
	for(int i:v)printf(" %d",i);
	printf("\n");
	fflush(stdout);
	char str[5];
	scanf("%s",str);
	if(str[0]=='Y')return 1;
	return 0;
}

int main(){
	scanf("%d",&n);
	int a,b;
	for(int i=1;i<n;i++){
		scanf("%d%d",&a,&b);
		adj[a].pb(b);adj[b].pb(a);
	}
	for(int i=1;i<=n;i++){
		if(SZ(adj[i])>=3){
			dfs(i,0);
			break;
		}
	}
	if(query(leaf)){
		vector<int> cur;
		for(int i:leaf)cur.pb(i);
		while(SZ(cur)>1){
			int m=SZ(cur)/2;
			set<int> s;
			for(int i=0;i<m;i++)s.insert(cur[i]);
			vector<int> v;
			for(int i:leaf){
				if(s.find(i)==s.end())v.pb(i);
			}
			if(query(v)){
				cur.clear();
				for(int i:s)cur.pb(i);
			}else{
				v.clear();
				for(int i=m;i<SZ(cur);i++)v.pb(cur[i]);
				cur.clear();
				for(int i:v)cur.pb(i);
			}
		}
		printf("! %d\n",cur[0]);
		fflush(stdout);
		return 0;
	}
	
	findFakeRoot();
	dfs(fr,0);
	int l=0,r=maxdep-1;
	while(l<r){
		int m=(l+r+1)/2;
		vector<int> v;
		for(int i=1;i<=n;i++){
			if(dep[i]>=m)v.pb(i);
		}
		for(int i:leaf)v.pb(i);
		if(query(v))l=m;
		else r=m-1;
	}
	if(l==0){
		printf("! %d\n",fr);
		fflush(stdout);
		return 0;
	}
	vector<int> v,perm;
	for(int i=1;i<=n;i++){
		if(dep[i]==l&&SZ(adj[i])>1)v.pb(i);
	}
	if(SZ(v)==1){
		printf("! %d\n",v[0]);
		fflush(stdout);
		return 0;
	}
	while(SZ(v)>1){
		int m=SZ(v)/2;
		vector<int> q;
		for(int i=0;i<m;i++){
			q.pb(v[i]);
		}
		for(int i:leaf)q.pb(i);
		if(query(q)){
			for(int i=m;i<SZ(v);i++)v.pop_back();
		}else{
			reverse(ALL(v));
			for(int i=0;i<m;i++)v.pop_back();
			reverse(ALL(v));
		}
	}
	printf("! %d\n",v[0]);
	fflush(stdout);
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 7
Accepted
time: 1ms
memory: 3696kb

input:

7
4 1
1 2
4 3
3 5
3 6
4 7
NO
YES
NO

output:

? 4 2 5 6 7
? 7 1 2 3 4 5 6 7
? 6 1 2 3 5 6 7
! 4

result:

ok OK

Test #2:

score: -7
Wrong Answer
time: 1ms
memory: 3612kb

input:

9
5 9
8 6
2 8
1 8
3 6
6 7
1 4
4 5
YES
NO
YES

output:

? 4 2 3 7 9
? 2 3 7
? 3 2 7 9
! 3

result:

wrong output format Unexpected end of file - int32 expected

Subtask #2:

score: 0
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 1ms
memory: 3704kb

input:

30
1 15
29 30
1 4
7 28
29 17
1 26
26 7
12 5
27 13
3 7
27 1
21 15
9 22
22 5
24 27
19 1
25 30
22 27
6 15
16 13
18 2
27 10
27 30
20 26
8 15
18 8
14 1
27 23
11 3
YES
YES
YES
YES
YES

output:

? 17 2 4 6 9 10 11 12 14 16 17 19 20 21 23 24 25 28
? 9 9 10 12 14 17 19 23 24 25
? 13 9 10 12 14 16 17 19 20 21 23 24 25 28
? 15 6 9 10 11 12 14 16 17 19 20 21 23 24 25 28
? 16 4 6 9 10 11 12 14 16 17 19 20 21 23 24 25 28
! 2

result:

wrong output format Unexpected end of file - int32 expected

Subtask #3:

score: 0
Wrong Answer

Test #54:

score: 61
Acceptable Answer
time: 6ms
memory: 3712kb

input:

500
419 133
44 225
391 269
419 461
293 347
108 31
110 363
423 257
321 155
498 87
180 492
251 5
357 30
341 172
275 109
372 446
286 336
208 339
162 320
138 103
129 219
62 141
359 286
130 238
470 460
418 48
210 358
429 13
323 143
382 415
406 394
309 175
325 170
128 108
6 113
363 17
470 457
7 224
288 48...

output:

? 252 2 3 7 8 10 11 12 13 15 16 19 24 26 28 29 33 34 36 37 38 39 40 41 42 43 45 46 47 50 53 54 55 57 58 64 65 67 68 69 70 71 72 73 74 75 76 77 78 79 80 82 83 85 86 88 95 96 99 100 109 110 112 114 117 119 121 122 123 124 126 129 134 135 138 140 144 148 149 150 152 153 154 155 158 164 165 167 168 169 ...

result:

points 0.7349397590 OK

Test #55:

score: 0
Wrong Answer
time: 3ms
memory: 3708kb

input:

500
188 321
193 4
334 269
259 66
121 396
73 153
332 477
263 67
178 262
185 377
175 53
462 245
390 337
387 200
445 92
387 159
135 263
323 312
143 374
252 47
375 382
303 345
345 283
150 1
66 289
462 82
317 201
169 423
154 193
486 251
368 305
357 375
107 443
437 348
64 55
408 465
315 469
186 328
197 39...

output:

? 240 3 8 9 10 11 12 14 17 20 21 22 25 26 27 28 30 31 33 35 40 43 45 49 50 51 54 55 56 57 59 60 61 62 63 65 67 68 69 70 72 75 76 78 79 83 84 85 86 87 88 89 90 91 92 94 96 99 100 102 103 105 106 109 113 116 119 124 125 126 127 129 130 131 132 138 139 140 142 143 145 148 149 154 155 157 163 167 168 17...

result:

wrong output format Unexpected end of file - int32 expected