結果

問題 No.3237 Find the Treasure!
ユーザー shobonvip
提出日時 2025-08-16 17:49:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 325 ms / 3,000 ms
コード長 2,977 bytes
コンパイル時間 4,034 ms
コンパイル使用メモリ 258,128 KB
実行使用メモリ 25,972 KB
平均クエリ数 13.83
最終ジャッジ日時 2025-08-16 17:49:59
合計ジャッジ時間 12,915 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
	author:  shobonvip
	created: 2025.08.16 17:40:06
**/

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

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n;cin>>n;
	vector ikeru(n,vector<int>(0));
	vector<int> u(n-1),v(n-1);
	rep(i,0,n-1){
		cin>>u[i]>>v[i];
		u[i]--;v[i]--;
		ikeru[u[i]].push_back(v[i]);
		ikeru[v[i]].push_back(u[i]);
	}

	vector<int> color(n);
	auto dfs=[&](auto self, int i, int p) -> void {
		for (int j: ikeru[i]) {
			if (j == p) continue;
			color[j] = color[i] ^ 1;
			self(self, j, i);
		}
	};
	
	dfs(dfs, 0, -1);

	auto give_query=[&](vector<int> q) -> bool {
		cout << '?';
		for (int i=0; i<n-1; i++) {
			cout << ' ' << q[i]+1;
		}
		cout << endl;
		string s; cin >> s;
		if (s == "Yes") return true;
		return false;
	};

	vector<bool> is_candidate(n);
	vector<int> que(n-1);
	rep(i,0,n-1){
		if(color[u[i]]==1) que[i]=u[i];
		else que[i]=v[i];
	}

	if (give_query(que)) {
		rep(i,0,n) {
			if (color[i] == 1) {
				is_candidate[i] = 1;
			}
		}
	}else{
		rep(i,0,n) {
			if (color[i] == 0) {
				is_candidate[i] = 1;
			}
		}
	}

	auto check = [&]() -> bool {
		int cnt = 0;
		rep(i,0,n) {if (is_candidate[i]) cnt++;}
		return cnt > 1;
	};

	while (check()) {
		vector<int> candidates;
		rep(i,0,n) {
			if (is_candidate[i]) {
				candidates.push_back(i);
			}
		}
		int t = candidates.size() / 2;
		vector<bool> new_candidate_true(n);
		vector<bool> new_candidate_false(n);
		rep(i,0,t) {
			new_candidate_true[candidates[i]] = 1;
		}
		rep(i,t,(int)candidates.size()) {
			new_candidate_false[candidates[i]] = 1;
		}

		vector<int> que(n-1);
		rep(i,0,n-1) {
			if (new_candidate_true[u[i]]) que[i] = u[i];
			else if (new_candidate_false[v[i]]) que[i] = u[i];
			else que[i] = v[i];
		}

		if (give_query(que)) is_candidate = new_candidate_true;
		else is_candidate = new_candidate_false;

	}

	rep(i,0,n) {
		if (is_candidate[i]) {
			cout << '!' << ' ' << i+1 << endl;
			return 0;
		}
	}
}

0