fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1044-C (Div. 2) The Nether
최초 업로드: 2025-09-11 18:35:18
최근 수정 시간: 2025-09-11 19:13:24
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 13
# C. The Nether [문제 링크](https://codeforces.com/contest/2133/problem/C) ## Problem Statement *This is an interactive problem.* Having recently discovered The Nether, Steve has built a network of $n$ nether portals, each at a different location in his world. Each portal is connected in one direction to some number (possibly zero) of other portals. To avoid getting lost, Steve has carefully built the network of portals so that there is no sequence of jumps through portals that will bring you from a location back to itself; formally, the network forms a directed acyclic graph. Steve refuses to tell you which portals are connected to which, but he will allow you to ask queries. In each query, you give Steve a set of locations $S$ = {$s_1,s_2,\ldots,s_k$} and a starting location $x\in S$. Steve will figure out the longest path starting at $x$ that only passes through locations in $S$ and tell you the number of locations in it. (If it is impossible to reach any other location in $S$ from $x$, Steve will reply with $1$.) As you are looking to obtain the achievement “Hot Tourist Destinations”, you want to find any path that visits as many locations as possible. Steve is feeling particularly generous and will give you $2\cdot n$ queries to find it. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 1000$). The description of the test cases follows. The only line of each test case contains a single integer $n$ ($2\le n\le 500$) — the number of locations. It is guaranteed that the sum of $n^3$ over all test cases does not exceed $500^3$. ## Interaction The interaction for each test case begins with reading the integer $n$. Then you can make up to $2\cdot n$ queries. To make a query, output a line in the following format: - $? x k s_1 s_2 \ldots s_k$ The jury will return the answer to the query. When you find a path with maximum length, output a single line in the following format: - $! k v_1 v_2 \ldots v_k$ This denotes starting at portal $v_1$, then jumping to $v_2$, then to $v_3$ and so on, ending at $v_k$. Note that outputting the answer **does not count** towards the limit of $2\cdot n$ queries. After printing each query do not forget to output the end of line and flush$^{\ast}$ the output. Otherwise, you will get `Idleness limit exceeded` verdict. If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive `Wrong answer` because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream. The interactor is **not adaptive**. The connections between portals do not change during the interaction. ## Hacks To perform a hack, use the following format: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 1000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\le n\le 500$) — the number of locations. Then, output $n$ lines. The $i$-th line should be of the form $k_i\,v_1\,v_2\,\ldots\,v_{k_i}$ ($0\le k_i\le n-1$, $1\le v_j\le n$, $v_j\ne i$), denoting that the portal at location $i$ is connected in one direction (leads to) locations $v_1,v_2,\ldots,v_{k_i}$. The given network must form a directed acyclic graph. The sum of $n^3$ over all test cases must not exceed $500^3$. ## Footnotes To flush, use: - `fflush(stdout)` or `cout.flush()` in C++; - `sys.stdout.flush()` in Python; - see the documentation for other languages. ## 풀이 n번의 질문으로 가장 깊은 노드를 찾고, 깊이 기준으로 내림차순으로 정렬한 후, 해당 깊이에서 노드가 선택되었거나 각 노드를 제외했을 때, 최대 깊이가 달라지지 않으면 삭제한다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int n; cin >> n; int deepestIdx, deepestDepth=0; vector<pair<int, int>> candidate; for(int i=1;i<=n;i++) { cout << "? " << i << ' ' << n; for(int j=1;j<=n;j++) cout << ' ' << j; cout << '\n' << flush; int depth; cin >> depth; if(deepestDepth<depth) { deepestDepth = depth; deepestIdx = i; } candidate.push_back({i, depth}); } sort(candidate.begin(), candidate.end(), [](auto x, auto y) {return x.second>y.second;}); for(int i=0;i<candidate.size();i++) { auto cur = candidate[i]; if(cur.second==deepestDepth) { if(cur.first!=deepestIdx) candidate.erase(find(candidate.begin(), candidate.end(), cur)), i--; continue; } cout << "? " << deepestIdx << ' ' << candidate.size()-1; for(auto e : candidate) { if(cur.first!=e.first) cout << ' ' << e.first; } cout << '\n' << flush; int inputDepth; cin >> inputDepth; if(deepestDepth==inputDepth || i>0 && candidate[i].second==candidate[i-1].second) candidate.erase(find(candidate.begin(), candidate.end(), cur)), i--; } cout << "! " << candidate.size(); for(auto e : candidate) cout << ' ' << e.first; cout << '\n' << flush; } } ```