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

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut