1592D - Hemose in ICPC - CodeForces Solution


binary search dfs and similar implementation interactive math number theory trees *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define N 502000

using namespace std;
struct edge{
    int to,next;
}e[N<<1];
const int mo=1000000007;
inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int n,head[N],cnt,c[N],tot;
pii ed[N];
void add(int u,int v){
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u,int fa){
    for (int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if (v==fa)continue;
        ed[++tot]=(pii){u,v};
        dfs(v,u);
    }
}
int ck(int l,int r){
    int sum=0;
    for (int i=1;i<=n;++i)c[i]=0;
    for (int i=l;i<=r;++i)c[ed[i].fi]=c[ed[i].se]=1;
    for (int i=1;i<=n;++i)sum+=c[i];
    cout<<"? "<<sum;
    for (int i=1;i<=n;++i)if (c[i])cout<<" "<<i;
    cout<<endl;
    int x;cin>>x;
    return x;
}
signed main(){
    n=read();int mx=0;
    for (int i=1;i<n;++i){
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    cout<<"? "<<n;
    for (int i=1;i<=n;++i)cout<<" "<<i;cout<<endl;
    cin>>mx;
    int l=1,r=n-1,res;
    while (l<=r){
        int mid=(l+r)>>1;
        if (ck(l,mid)==mx)r=mid-1,res=mid;
        else l=mid+1,res=mid+1;
    }
    cout<<"! "<<ed[res].fi<<" "<<ed[res].se<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum