Codeforces gym101755F Tree Restoration(拓扑排序)

题意:

一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树

n<=1000

思路:

对祖先->后代建立有向图,跑拓扑排序。跑的时候不断更新父亲并判断答案的存在性,同时注意一种情况:一个点他儿子是他的后代,同样也得是他父亲的后代,这样传递下去就一定是所有祖宗的后代。

代码:

代码参考网上的

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e6+;
const int maxm = 1e6+;
//const int inf = 0x3f3f3f3f;
const int inf = 1e9+;
const db pi = acos(-1.0); int n;
vector<int>v[maxn];
int f[maxn];
int ma[][];
int d[maxn];
int root;
bool sv(){
queue<int>q;
for(int i = ; i <= n; i++){
if(d[i]==)q.push(i);
}
if(q.size()!=)return false;
root = q.front();
while(!q.empty()){
int top = q.front();
q.pop();
for(int i = ; i < v[top].size(); i++){
int x = v[top][i];
d[x]--;
if(f[x]!=-&&!ma[f[x]][top])return false;
f[x] = top;
if(d[x]==)q.push(x);
}
} for(int i = ; i <= n; i++){
if(d[i]!=)return false;
}f[root]=;
for(int i = ; i <= n; i++){
//if(i!=root){
for(int j = ; j < v[i].size(); j++){
if(!ma[f[i]][v[i][j]]){
//printf("--%d %d %d\n",i,f[i],v[i][j]);
return false;
}
}
//}
}
return true;
}
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++){
ma[][i] = ;
f[i] = -;
int k;
scanf("%d", &k);
for(int j = ; j <= k; j++){
int x;
scanf("%d", &x);
v[i].pb(x);
d[x]++;
ma[i][x]=;
}
}
mem(f,-); if(sv()){printf("YES\n");
for(int i = ; i <= n; i++){
if(i==root)continue;
printf("%d %d\n",f[i],i);
}}
else printf("NO");
return ;
}
/*
4
2 2 3
2 3 4
1 4
0
*/
上一篇:servlet中获取request中文乱码问题分析


下一篇:【EasyUI学习-2】Easyui Tree的异步加载