Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<int> v[200005];
- int sz[200005],dep[200005],depx;
- int pre(int node,int p)
- {
- sz[node]=1;
- for (int u:v[node])
- {
- if (u!=p)
- {
- dep[u]=dep[node]+1;
- sz[node]+=pre(u,node);
- }
- }
- return sz[node];
- }
- vector<int> h;
- int query(char c,int u)
- {
- printf("%c %d\n",c,u);
- fflush(stdout);
- int ans;
- scanf("%d",&ans);
- return ans;
- }
- void get(int node)
- {
- h.push_back(node);
- int big=-1;
- for (int u:v[node])
- {
- if (sz[node]>sz[u] && (big==-1 || (sz[u]>sz[big])))
- big=u;
- }
- if (big!=-1)
- get(big);
- }
- int dfs(int node)
- {
- h.clear();
- get(node);
- int depy=(depx+dep[h.back()]-query('d',h.back()))/2,y=h[depy-dep[node]];
- if (depx==depy)
- return y;
- return dfs(query('s',y));
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- for (int i=1;i<n;i++)
- {
- int a,b;
- scanf("%d%d",&a,&b);
- v[a].push_back(b);
- v[b].push_back(a);
- }
- depx=query('d',1);
- pre(1,0);
- printf("! %d\n",dfs(1));
- fflush(stdout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement