题目链接:
题意:给出一个无环图( 也就是树,但是也有可能是森林),代表一个国家的城市。1是首都。每条边是一条公路。现在要在这些公路中选出一些修改成铁路。要求每个城市至多在一条铁路上。修改完后,每个城市到首都可能是铁路公路交叉着走。现在定义一个城市的“不便利度”为到达首都要走的公路的次数。定义国家的不便利度为所有城市不便利度的最大值。问国家的不便利度最小为多少?有多少种修改方案使得可以得到此最小不便利度?
思路:f[n][10][3],f[i][j][k]表示以i为根的子树,其所有孩子到其的不便利度最大为j,向其孩子修建k条铁路的方案数,设i的孩子集合为S,那么有:
化简后得到:
#include <iostream>
#include <cstdio>#include <string.h>#include <algorithm>#include <cmath>#include <vector>#include <queue>#include <set>#include <stack>#include <string>#include <map>#include <time.h> #define abs(x) ((x)>=0?(x):-(x))#define i64 long long#define u32 unsigned int#define u64 unsigned long long#define clr(x,y) memset(x,y,sizeof(x))#define CLR(x) x.clear()#define ph(x) push(x)#define pb(x) push_back(x)#define Len(x) x.length()#define SZ(x) x.size()#define PI acos(-1.0)#define sqr(x) ((x)*(x))#define MP(x,y) make_pair(x,y)#define EPS 1e-6 #define FOR0(i,x) for(i=0;i<x;i++)#define FOR1(i,x) for(i=1;i<=x;i++)#define FOR(i,a,b) for(i=a;i<=b;i++)#define FORL0(i,a) for(i=a;i>=0;i--)#define FORL1(i,a) for(i=a;i>=1;i--)#define FORL(i,a,b)for(i=a;i>=b;i--) #define rush() int CC;for(scanf("%d",&CC);CC--;)#define Rush(n) while(scanf("%d",&n)!=-1)using namespace std; void RD(int &x){scanf("%d",&x);}void RD(i64 &x){scanf("%lld",&x);}void RD(u64 &x){scanf("%I64u",&x);}void RD(u32 &x){scanf("%u",&x);}void RD(double &x){scanf("%lf",&x);}void RD(int &x,int &y){scanf("%d%d",&x,&y);}void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}void RD(char &x){x=getchar();}void RD(char *s){scanf("%s",s);}void RD(string &s){cin>>s;} void PR(int x) {printf("%d\n",x);}void PR(int x,int y) {printf("%d %d\n",x,y);}void PR(i64 x) {printf("%lld\n",x);}void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}void PR(u32 x) {printf("%u\n",x);}void PR(u64 x) {printf("%llu\n",x);}void PR(double x) {printf("%.6lf\n",x);}void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}void PR(char x) {printf("%c\n",x);}void PR(char *x) {printf("%s\n",x);}void PR(string x) {cout<<x<<endl;} const int mod=100000007;const i64 inf=((i64)1)<<40;const double dinf=1000000000000000000.0;const int INF=2000000000;const int N=100005; vector<int> g[N];i64 f[N][11][3];int n,m,Q;int visit[N];void dfs(int u){ visit[u]=1; int i,v; FOR0(i,SZ(g[u])) { v=g[u][i]; if(!visit[v]) dfs(v); }}i64 get(i64 x){ if(x%Q!=0) return x%Q; if(x!=0) return Q; return 0;}void DFS(int u,int pre,int b){ f[u][b][0]=1; f[u][b][1]=0; f[u][b][2]=0; int i,v; i64 f1,f2; FOR0(i,SZ(g[u])) { v=g[u][i]; if(v==pre) continue; DFS(v,u,b); f1=f[v][b][0]+f[v][b][1]; if(!b) f2=0; else f2=f[v][b-1][0]+f[v][b-1][1]+f[v][b-1][2]; f[u][b][2]=get(f[u][b][2]*f2+f[u][b][1]*f1); f[u][b][1]=get(f[u][b][1]*f2+f[u][b][0]*f1); f[u][b][0]=get(f[u][b][0]*f2); }}int main(){ RD(n,m,Q); int i,x,y; FOR1(i,m) { RD(x,y); g[x].pb(y); g[y].pb(x); } dfs(1); FOR1(i,n) if(!visit[i]) { puts("-1"); puts("-1"); return 0; } for(i=0;;i++) { DFS(1,-1,i); if(f[1][i][0]+f[1][i][1]+f[1][i][2]) { PR(i); PR((f[1][i][0]+f[1][i][1]+f[1][i][2])%Q); return 0; } } puts("-1"); puts("-1");}