头插法创建链表
定义一个结构体,包含当前的值域和指针域
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node
{
node *next;
int where;
} *first[N],a[N];
int l,n;
int f[N];
inline void makelist(int x,int y){
a[++l].where = y;
a[l].next = first[x];
first[x] = &a[l];
}
inline void dfs(int i){
f[i]=1;
for(node *x=first[i];x;x=x->next){
dfs(x->where);
f[i]+=f[x->where];
}
}
inline void solve(){
cin>>n;
memset(first,0,sizeof first);
l=0;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
makelist(x,y);
}
}
int main(){
int t=1;
while(t--){
solve();
}
return 0;
}