1 solutions

  • 1
    @ 2025-12-14 11:28:45
    #include<bits/stdc++.h>
    using namespace std;
    int n,e[110],ne[110],h[110],id,a,b;
    bool st[110],f;
    void add(int a,int b)
    {
    	e[id]=b,ne[id]=h[a],h[a]=id++;
    }
    void dfs(int x)
    {
    	st[x]=true;
    	for(int j=h[x];j!=-1;j=ne[j])
    	{
    		if(!st[e[j]])
    		{
    			dfs(e[j]);
    		}
    	}
    }
    int main()
    {
    	cin>>n;
    	memset(h,-1,sizeof h);
    	for(int i=1;i<n;i++)
    	{
    		cin>>a>>b;
    		add(b,a);
    	}
    	for(int i=1;i<=n;i++)
    	{
    		memset(st,false,sizeof st);
    		f=true;
    		dfs(i);
    		for(int j=1;j<=n;j++)
    		{
    			if(!st[j])
    			{
    				f=false;
    				break;
    			}
    		}
    		if(f)
    		{
    			cout<<i;
    			return 0;
    		}
    	}
    	cout<<-1;
    	return 0;
    }
    
    • 1

    Information

    ID
    781
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    5
    Tags
    # Submissions
    7
    Accepted
    4
    Uploaded By