CF 1005F Berland and the Shortest Paths
最短路树
1 #include<queue> 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 int n,m,cnt,tot,num,k; 8 vector<int>cty[200005]; 9 int head[200005]; 10 int fa[200005]; 11 int dis[200005]; 12 int usd[200005]; 13 struct Edge{ 14 int fr; 15 int to; 16 int id; 17 int nxt; 18 }edge[400005]; 19 void init(){ 20 tot=1; 21 memset(dis,0x3f,sizeof(dis)); 22 memset(head,-1,sizeof(head)); 23 } 24 void addedge(int f,int t,int x){ 25 cnt++; 26 edge[cnt].fr=f; 27 edge[cnt].to=t; 28 edge[cnt].id=x; 29 edge[cnt].nxt=head[f]; 30 head[f]=cnt; 31 } 32 void bfs(){ 33 dis[1]=0; 34 queue<int>que; 35 que.push(1); 36 while(!que.empty()){ 37 int s=que.front(); 38 que.pop(); 39 for(int i=head[s];i!=-1;i=edge[i].nxt){ 40 int v=edge[i].to; 41 if(dis[v]>dis[s]+1){ 42 dis[v]=dis[s]+1; 43 cty[v].push_back(edge[i].id); 44 que.push(v); 45 } 46 else if(dis[v]==dis[s]+1){ 47 cty[v].push_back(edge[i].id); 48 } 49 } 50 } 51 } 52 void dfs(int dep){ 53 if(dep==n+1){ 54 for(int i=1;i<=m;i++){ 55 printf("%d",usd[i]); 56 } 57 printf("\n"); 58 num++; 59 if(num==tot){ 60 exit(0); 61 } 62 return; 63 } 64 for(int i=0;i<cty[dep].size();i++){ 65 usd[cty[dep][i]]=1; 66 dfs(dep+1); 67 usd[cty[dep][i]]=0; 68 } 69 } 70 int main(){ 71 init(); 72 scanf("%d%d%d",&n,&m,&k); 73 for(int i=1;i<=m;i++){ 74 int u,v; 75 scanf("%d%d",&u,&v); 76 addedge(u,v,i); 77 addedge(v,u,i); 78 } 79 bfs(); 80 for(int i=2;i<=n;i++){ 81 tot*=cty[i].size(); 82 if(tot>=k){ 83 tot=k; 84 break; 85 } 86 } 87 printf("%d\n",tot); 88 dfs(2); 89 return 0; 90 }