BZOJ1086: [SCOI2005]王室联邦

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1086

题解:
膜拜POPOQQQ:http://blog.csdn.net/popoqqq/article/details/42772237

这样分出来满足题意,但是某些块并不是联通的。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000+5
14 #define maxm 100000+5
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
23 #define mod 1000000007
24 using namespace std;
25 inline int read()
26 {
27     int x=0,f=1;char ch=getchar();
28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
30     return x*f;
31 }
32 int n,m,tot,top,head[maxn],belong[maxn],sta[maxn],cnt,rt[maxn];
33 struct edge{int go,next;}e[2*maxn];
34 inline void add(int x,int y)
35 {
36     e[++tot]=(edge){y,head[x]};head[x]=tot;
37     e[++tot]=(edge){x,head[y]};head[y]=tot;
38 }
39 inline void dfs(int x,int fa)
40 {
41     int tmp=top;
42     for4(i,x)if(y!=fa)
43     {
44         dfs(y,x);
45         if(top-tmp>=m)
46         {
47            rt[++cnt]=x;
48            while(top!=tmp)belong[sta[top--]]=cnt;
49         }
50     }
51     sta[++top]=x;
52 }
53 int main()
54 {
55     freopen("input.txt","r",stdin);
56     freopen("output.txt","w",stdout);
57     n=read();m=read();
58     for1(i,n-1)add(read(),read());
59     dfs(1,0);
60     while(top)belong[sta[top--]]=cnt;
61     printf("%d\n",cnt);
62     for1(i,n)printf("%d%c",belong[i],i==n?'\n':' ');
63     for1(i,cnt)printf("%d%c",rt[i],i==cnt?'\n':' ');
64     return 0;
65 }
View Code

 

posted @ 2015-01-25 09:45  ZYF-ZYF  Views(268)  Comments(0Edit  收藏  举报