D. Lost Tree - 交互 - 二分图染色

 

D. Lost Tree

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an interactive problem.

Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of nn nodes! The nodes of the tree are numbered from 11 to nn.

The game master only allows him to ask one type of question:

  • Little Dormi picks a node rr (1≤r≤n1≤r≤n), and the game master will reply with an array d1,d2,…,dnd1,d2,…,dn, where didi is the length of the shortest path from node rr to ii, for all 1≤i≤n1≤i≤n.

Additionally, to make the game unfair challenge Little Dormi the game master will allow at most ⌈n2⌉⌈n2⌉ questions, where ⌈x⌉⌈x⌉ denotes the smallest integer greater than or equal to xx.

Faced with the stomach-churning possibility of not being able to guess the tree, Little Dormi needs your help to devise a winning strategy!

Note that the game master creates the tree before the game starts, and does not change it during the game.

Input

The first line of input contains the integer nn (2≤n≤20002≤n≤2000), the number of nodes in the tree.

You will then begin interaction.

Output

When your program has found the tree, first output a line consisting of a single "!" followed by n−1n−1 lines each with two space separated integers aa and bb, denoting an edge connecting nodes aa and bb (1≤a,b≤n1≤a,b≤n). Once you are done, terminate your program normally immediately after flushing the output stream.

You may output the edges in any order and an edge (a,b)(a,b) is considered the same as an edge (b,a)(b,a). Answering is not considered as a query.

Interaction

After taking input, you may make at most ⌈n2⌉⌈n2⌉ queries. Each query is made in the format "? r", where rr is an integer 1≤r≤n1≤r≤n that denotes the node you want to pick for that query.

You will then receive nn space separated integers d1,d2,…,dnd1,d2,…,dn, where didi is the length of the shortest path from node rr to ii, followed by a newline.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

If at any point you make an invalid query or try to make more than ⌈n2⌉⌈n2⌉ queries, the interaction will terminate immediately and you will receive a Wrong Answer verdict.

Hacks

To hack a solution, use the following format.

The first line contains the integer nn (2≤n≤20002≤n≤2000).

The next n−1n−1 lines contain two integers uu and vv (1≤u,v≤n1≤u,v≤n) denoting an edge between uu and vv (u≠vu≠v). These n−1n−1 edges must form a tree.

Examples

input

Copy

4

0 1 2 2

1 0 1 1

output

Copy

? 1

? 2

!
4 2
1 2
2 3

input

Copy

5

2 2 1 1 0

output

Copy

? 5

!
4 5
3 5
2 4
1 3

Note

Here is the tree from the first example.

Notice that the edges can be output in any order.

Additionally, here are the answers for querying every single node in example 11:

  • 11: [0,1,2,2][0,1,2,2]
  • 22: [1,0,1,1][1,0,1,1]
  • 33: [2,1,0,2][2,1,0,2]
  • 44: [2,1,2,0][2,1,2,0]

Below is the tree from the second example interaction.

Lastly, here are the answers for querying every single node in example 22:

  • 11: [0,4,1,3,2][0,4,1,3,2]
  • 22: [4,0,3,1,2][4,0,3,1,2]
  • 33: [1,3,0,2,1][1,3,0,2,1]
  • 44: [3,1,2,0,1][3,1,2,0,1]
  • 55: [2,2,1,1,0][2,2,1,1,0]

Codeforces (c) C

=========================================================================

n个节点,n/2次,n-1条边,也就是一次询问获得两条边,自然考虑二分图染色法,任取一个点1作为根节点。每次询问与当前节点距离为1的点就是一条边,,由于我们强制要求了1号节点必须选择,我们二分图染色式询问时,应该事先统计一下那种奇偶性的节点最少,比如下图,强制1必须选时,虽然选奇选偶效果一样,但是还有一定差距的

 

# include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

int edge[2010][2010];
int depth[2010];
int book[2010];

vector<int>v;
int main ()
{

    int n;
    cin>>n;
    int nowcnt=0;

    cout<<"? "<<1<<endl;

    int maxx=0;
    
    
    int cntji=0,cntou=0;
    for(int i=1;i<=n;i++)
    {
        cin>>depth[i];
        maxx=max(maxx,depth[i]);
        if(depth[i])
        {
            cntji+=(depth[i]%2==1);
            cntou+=(depth[i]%2==0);
        }
        if(depth[i]==1)
        {
            edge[1][i]=1;
            edge[i][1]=1;
        }


    }
    if(n==2)
    {
        cout<<"!"<<endl;

        cout<<1<<" "<<2<<endl;

        return 0;
    }
    else
    {
        if(maxx==1)
        {
            cout<<"!"<<endl;
            for(int i=2;i<=n;i++)
            {
                cout<<1<<" "<<i<<endl;
            }

            return 0;
        }
        else if(maxx==2)
        {

            for(int i=2;i<=n;i++)
            {
                if(depth[i]==1)
                {
                    v.push_back(i);
                }
            }

            for(auto it:v)
            {
               cout<<"?"<<" "<<it<<endl;

               for(int i=1;i<=n;i++)
               {
                   int x;
                   cin>>x;

                   if(x==1)
                   {
                       edge[it][i]=1;
                       edge[i][it]=1;
                   }
               }
            }

             cout<<"!"<<endl;
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(edge[i][j])
                    {
                        cout<<i<<" "<<j<<endl;
                    }
                }
            }

            return 0;
        }
        else
        {
            int fuck=1;
            
            if(cntji>cntou)
                fuck=0;

            for(int i=2;i<=n;i++)
            {
                if(depth[i]%2==fuck)
                {
                    v.push_back(i);
                }
            }


             for(auto it:v)
            {
               cout<<"?"<<" "<<it<<endl;

               for(int i=1;i<=n;i++)
               {
                   int x;
                   cin>>x;

                   if(x==1)
                   {
                       edge[it][i]=1;
                       edge[i][it]=1;
                   }
               }
            }

              cout<<"!"<<endl;
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(edge[i][j])
                    {
                        cout<<i<<" "<<j<<endl;
                    }
                }
            }

            return 0;
        }

    }



    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值