1. 程式人生 > >codeforces 1041 E. Tree Reconstruction 和度數有關的構造樹

codeforces 1041 E. Tree Reconstruction 和度數有關的構造樹

cos lock .com lis 如果 原來 c++ pla max

CF 1041E:http://codeforces.com/contest/1041/problem/E

題意:

    告訴你一個樹的節點個數,顯然有n-1條邊。已知去掉一條邊後,兩個集合中最大的節點值。問原來的樹形狀是怎麽樣的,構造不出來就輸出NO。

思路:

    這裏說的“度數”可能有點不恰當。指以這個點引出一條鏈的長度,鏈上點的值小於這個點。

    我想著這應該是可以作為一條鏈的,但是一直沒有想到向節點度數上去想。首先,輸入的一對值中,有一個一定是等於n的,那另一個值我們給它度數++。我們把度數為0的點從大到小加入到隊列中。然後枚舉度數大於1的點,從隊列中取出較為自由的點當作鏈上的點。註意,如果自由的最大點比當前點要大,那麽肯定是不存在的。

 

技術分享圖片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    
<vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000")
//c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 // const int mod = 10007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2000; int n; int du[maxn]; queue<int>que; vector<pii>ans; int main(){ cin>>n; int flag=1,cnt = 0; for(int i=1; i<n; i++){ int u,v; cin>>u>>v; if(u<n && v<n){ puts("NO"); return 0; } if(u == n)du[v]++; else du[u] ++; } for(int i=n-1; i>=1; i--){ if(du[i] == 0)que.push(i); } for(int i=n-1; i>=1; i--){ if(du[i] == 0)continue; du[i] --; int u = i; while(du[i]--){ if(que.empty()||que.front() > i){ //無法成鏈,NO puts("NO"); return 0; } int v = que.front();que.pop(); ans.pb(pii(v, u)); u = v; } ans.pb(pii(u,n)); } puts("YES"); for(int i=0; i<ans.size(); i++){ printf("%d %d\n", ans[i].fi, ans[i].se); } return 0; }
CF 1041E

codeforces 1041 E. Tree Reconstruction 和度數有關的構造樹