Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<bitset>
#include<assert.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
	char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
void open2(const char *s){
#ifdef DEBUG
	char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
template <class T>
int upmin(T &a, const T &b){return (b<a?a=b,1:0);}
template <class T>
int upmax(T &a, const T &b){return (b>a?a=b,1:0);}
namespace io
{
	const int SIZE=(1<<20)+1;
	char ibuf[SIZE],*iS,*iT;
	char obuf[SIZE],*oS=obuf,*oT=oS+SIZE-1;
	int getc()
	{
		(iS==iT?iS=ibuf,iT=ibuf+fread(ibuf,1,SIZE,stdin):0);
		return iS==iT?EOF:*(iS++);
	}
	int f;
	char c;
	template <class T>
	void get(T &x)
	{
		f=1;
		for(c=getc();(c<'0'||c>'9')&&c!='-';c=getc());
		(c=='-'?f=-1,c=getc():0);
		x=0;
		for(;c>='0'&&c<='9';c=getc())
			x=x*10+c-'0';
		x*=f;
	}
	void flush()
	{
		fwrite(obuf,1,oS-obuf,stdout);
		oS=obuf;
	}
	void putc(char x)
	{
		*(oS++)=x;
		if(oS==oT)
			flush();
	}
	int a[55],t;
	template <class T>
	void put(T x)
	{
		if(!x)
			putc('0');
		x<0?putc('-'),x=-x:0;
		while(x)
		{
			a[++t]=x%10;
			x/=10;
		}
		while(t)
			putc(a[t--]+'0');
	}
	void space()
	{
		putc(' ');
	}
	void enter()
	{
		putc('\n');
	}
	struct flusher
	{
		~flusher()
		{
			flush();
		}
	}
	io_flusher;
}
const int N=1000010;
vector<int> g[N];
int s1[N];
int s2[N];
void dfs(int x,int fa)
{
	s1[x]=0;
	int num=0;
	for(auto v:g[x])
		if(v!=fa)
		{
			dfs(v,x);
			s1[x]+=s1[v];
			if(!s1[v])
				num++;
		}
	if(num)
		s1[x]+=num-1;
}
void dfs2(int x,int fa)
{
	int temp1=s2[x];
	int temp2=(fa?(s2[x]?0:1):0);
	for(auto v:g[x])
		if(v!=fa)
		{
			temp1+=s1[v];
			temp2+=(s1[v]?0:1);
		}
	for(auto v:g[x])
		if(v!=fa)
		{
			s2[v]=temp1-s1[v]+(temp2-!s1[v]?temp2-!s1[v]-1:0);
			dfs2(v,x);
		}
}
int h[N];
int s3[N];
int b[N];
void dfs3(int x,int fa)
{
	s3[x]=0;
	int num=0;
	h[x]=x;
	for(auto v:g[x])
		if(v!=fa)
		{
			dfs3(v,x);
			s3[x]+=s3[v];
			if(!s3[v])
				num++;
			h[x]=h[v];
		}
	if(num)
	{
		s3[x]+=num-1;
		for(auto v:g[x])
			if(v!=fa)
			{
				if(!s3[v]&&num>1)
				{
					b[h[v]]=1;
					num--;
				}
			}
	}
}
void solve()
{
	int n;
	io::get(n);
	for(int i=1;i<=n;i++)
	{
		g[i].clear();
		b[i]=s1[i]=s2[i]=0;
	}
	int x,y;
	for(int i=1;i<n;i++)
	{
		io::get(x);
		io::get(y);
      x++;
      y++;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1,0);
	dfs2(1,0);
	pii ans(0x7fffffff,0);
	for(int i=1;i<=n;i++)
		ans=min(ans,pii(s1[i]+s2[i]+1,i));
	for(int i=1;i<=n;i++)
	{
		b[i]=0;
		s3[i]=0;
	}
	dfs3(ans.second,0);
	b[ans.second]=1;
	printf("%d\n",ans.first);
	if(num)
	{
		while(1)
			;
	}
}
int main()
{
	open2("g");
		solve();
	return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:201:12: error: 'num' was not declared in this scope; did you mean 'enum'?
  201 |         if(num)
      |            ^~~
      |            enum