Summer training round2 #6 (Training #26)
阿新 • • 發佈:2017-08-11
fin cin cto ide view 技術分享 分享 summer ()
A:樹遍歷 DFS 貪心 先從最遠的點開始判
如果某個點連有的算上自身的junction不小於二就ans++ return 0
連有的算上自身的junction為一 就return 1
#include <bits/stdc++.h> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MOD 1000000007 #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #defineView Codepb push_back #define pai pair<int,int> #define mid ((l+r)/2) #define lson l,mid,(rt<<1) #define rson mid+1,r,(rt<<1|1) //ugooo ll = long long; //ugooo ull= unsigned long long; //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que;typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pairint; const double eps=1e-8; const double PI=acos(-1); const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; const int maxn=200005; vector<int> t[maxn]; int a[maxn],f[maxn]; int ans=0; void dfs(int x,int pre) { int sum=0; int len=t[x].size();for(int i=0;i<len;i++) { int aim=t[x][i]; if(aim==pre) continue; dfs(aim,x); sum+=a[aim]; } sum+=f[x]; if(sum>=2) { ans++; a[x]=0; } else if(sum==1) a[x]=1; else a[x]=0; } int main() { int n; cin >> n ; int from,to; for(int i=1;i<n;i++) { scanf("%d %d",&from,&to); t[from].pb(to); t[to].pb(from); } int m; cin >> m ; int now; for(int i=1;i<=m;i++) { scanf("%d",&now); f[now]=1; } dfs(1,-1); for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; cout<<ans<<endl; return 0; }
B;SPFA
C:爆搜
D:並查集
E:模擬
Summer training round2 #6 (Training #26)