无根树转有根树

#include<cstdio>
#include<vector>
using namespace std;

const int maxn = 100;

vector<int>G[maxn];
int p[maxn];

void dfs(int x,int root)
{
    for(int i = 0;i < G[x].size();i++)
    {
        int v = G[x][i];
        if(v != root)
        {
            p[v] = x;
            dfs(v,x);
        }
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    int edge;
    scanf("%d",&edge);
    for(int i = 0;i < edge;i++)
    {
        int x1,x2;
        scanf("%d%d",&x1,&x2);
        G[x1].push_back(x2);
        G[x2].push_back(x1);
    }
    int root;
    scanf("%d",&root);
    dfs(root,-1);
    return 0;
}

这道题目的独特之处在于只保存边

posted @ 2019-03-09 16:11  Toretto瑞  阅读(132)  评论(0)    收藏  举报