Codeforces 723F st-Spanning Tree【贪心Kruskal】
2017-05-10 12:56
351 查看
F. st-Spanning Tree
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an undirected connected graph consisting of
n vertices and m edges. There are no loops and no multiple edges in the graph.
You are also given two distinct vertices s and
t, and two values ds and
dt. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex
s doesn't exceed ds, and the degree of the vertex
t doesn't exceed dt, or determine, that there is no such spanning tree.
The spanning tree of the graph
G is a subgraph which is a tree and contains all vertices of the graph
G. In other words, it is a connected graph which contains
n - 1 edges and can be obtained by removing some of the edges from
G.
The degree of a vertex is the number of edges incident to this vertex.
Input
The first line of the input contains two integers n and
m (2 ≤ n ≤ 200 000,
1 ≤ m ≤ min(400 000, n·(n - 1) / 2)) — the number of vertices and the number of edges in the graph.
The next m lines contain the descriptions of the graph's edges. Each of the lines contains two integers
u and v (1 ≤ u, v ≤ n,
u ≠ v) — the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.
The last line contains four integers s,
t, ds,
dt (1 ≤ s, t ≤ n,
s ≠ t, 1 ≤ ds, dt ≤ n - 1).
Output
If the answer doesn't exist print "No" (without quotes) in the only line of the output.
Otherwise, in the first line print "Yes" (without quotes). In the each of the next
(n - 1) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.
You can output edges in any order. You can output the ends of each edge in any order.
If there are several solutions, print any of them.
Examples
Input
Output
Input
Output
题目大意:
给你N个点,M条无向边,让你从中选出N-1条边,使得最终生成树中,点s的度数不超过ds,点t的度数不超过dt.
问是否存在一种构图方式。
思路:
1、首先我们可以将边分成两类:
①有s或者t作为一点的边:这类边的加入要有一定的条件。
②没有s或者t作为一点的边:这类边的加入不会影响结果,没有限定的条件,所以我们可以将其权值设定为0.
所以问题我们可以转化为给边设定权值的问题。剩余部分只要跑最小生成树贪心算法维护结果是否合法即可。
2、那么我们的问题关键点就在于如何设定类型①的边的权值。
接下来我们引入贪心思维,然后将问题再分成三类:
①ds>dt.那么我们肯定优先加入只有s点的边,然后次优先加入只有t的边,最后再加入既有s又有t的边。
过程 kruskal算法贪心加并查集维护即可。如果最终s点的度和t点的度都满足要求即为结果。
②ds<dt.那么我们肯定优先加入只有t点的边,然后次优先加入只有s的边,最后再加入既有s又有t的边。
③ds==dt.那么我们跑两遍即可,第一遍优先加入只有s点的边,然后次优先加入只有t的边,最后再加入既有s又有t的边。第二遍反过去,优先加入只有t点的边,然后次优先加入只有s的边,最后再加入既有s又有t的边。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
f[B]=A;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int s,t,ds,dt;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
scanf("%d%d%d%d",&s,&t,&ds,&dt);
int vals,valt;
if(ds>dt)vals=1,valt=2;
else vals=2,valt=1;
for(int i=0;i<m;i++)
{
a[i].use=0;
if(a[i].x==s||a[i].y==s)a[i].w=vals;
if(a[i].x==t||a[i].y==t)a[i].w=valt;
}
int cnt=0;
sort(a,a+m,cmp);
int dds=0,ddt=0;
for(int i=0;i<m;i++)
{
if(a[i].w==0)
{
if(find(a[i].x)!=find(a[i].y))
{
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
else
{
if(find(a[i].x)!=find(a[i].y))
{
if(a[i].x==s||a[i].y==s)
{
if(dds==ds)continue;
dds++;
}
if(a[i].x==t||a[i].y==t)
{
if(ddt==dt)continue;
ddt++;
}
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
}
if(dds<=ds&&ddt<=dt&&cnt==n-1)
{
printf("Yes\n");
for(int i=0;i<m;i++)
{
if(a[i].use==1)
printf("%d %d\n",a[i].x,a[i].y);
}
}
else //如果不行再反向跑一遍。
{
for(int i=1;i<=n;i++)f[i]=i;
int vals,valt;
if(ds>=dt)vals=1,valt=2;
else vals=2,valt=1;
for(int i=0;i<m;i++)
{
a[i].use=0;
if(a[i].x==s||a[i].y==s)a[i].w=vals;
if(a[i].x==t||a[i].y==t)a[i].w=valt;
}
int cnt=0;
sort(a,a+m,cmp);
int dds=0,ddt=0;
for(int i=0;i<m;i++)
{
if(a[i].w==0)
{
if(find(a[i].x)!=find(a[i].y))
{
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
else
{
if(find(a[i].x)!=find(a[i].y))
{
if(a[i].x==s||a[i].y==s)
{
if(dds==ds)continue;
dds++;
}
if(a[i].x==t||a[i].y==t)
{
if(ddt==dt)continue;
ddt++;
}
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
}
if(dds<=ds&&ddt<=dt&&cnt==n-1)
{
printf("Yes\n");
for(int i=0;i<m;i++)
{
if(a[i].use==1)
printf("%d %d\n",a[i].x,a[i].y);
}
}
else
{
printf("No\n");
}
}
}
}
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an undirected connected graph consisting of
n vertices and m edges. There are no loops and no multiple edges in the graph.
You are also given two distinct vertices s and
t, and two values ds and
dt. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex
s doesn't exceed ds, and the degree of the vertex
t doesn't exceed dt, or determine, that there is no such spanning tree.
The spanning tree of the graph
G is a subgraph which is a tree and contains all vertices of the graph
G. In other words, it is a connected graph which contains
n - 1 edges and can be obtained by removing some of the edges from
G.
The degree of a vertex is the number of edges incident to this vertex.
Input
The first line of the input contains two integers n and
m (2 ≤ n ≤ 200 000,
1 ≤ m ≤ min(400 000, n·(n - 1) / 2)) — the number of vertices and the number of edges in the graph.
The next m lines contain the descriptions of the graph's edges. Each of the lines contains two integers
u and v (1 ≤ u, v ≤ n,
u ≠ v) — the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.
The last line contains four integers s,
t, ds,
dt (1 ≤ s, t ≤ n,
s ≠ t, 1 ≤ ds, dt ≤ n - 1).
Output
If the answer doesn't exist print "No" (without quotes) in the only line of the output.
Otherwise, in the first line print "Yes" (without quotes). In the each of the next
(n - 1) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.
You can output edges in any order. You can output the ends of each edge in any order.
If there are several solutions, print any of them.
Examples
Input
3 3 1 2 2 3 3 1 1 2 1 1
Output
Yes 3 2 1 3
Input
7 8 7 4 1 3 5 4 5 7 3 2 2 4 6 1 1 2 6 4 1 4
Output
Yes 1 3 5 7 3 2 7 4 2 4 6 1
题目大意:
给你N个点,M条无向边,让你从中选出N-1条边,使得最终生成树中,点s的度数不超过ds,点t的度数不超过dt.
问是否存在一种构图方式。
思路:
1、首先我们可以将边分成两类:
①有s或者t作为一点的边:这类边的加入要有一定的条件。
②没有s或者t作为一点的边:这类边的加入不会影响结果,没有限定的条件,所以我们可以将其权值设定为0.
所以问题我们可以转化为给边设定权值的问题。剩余部分只要跑最小生成树贪心算法维护结果是否合法即可。
2、那么我们的问题关键点就在于如何设定类型①的边的权值。
接下来我们引入贪心思维,然后将问题再分成三类:
①ds>dt.那么我们肯定优先加入只有s点的边,然后次优先加入只有t的边,最后再加入既有s又有t的边。
过程 kruskal算法贪心加并查集维护即可。如果最终s点的度和t点的度都满足要求即为结果。
②ds<dt.那么我们肯定优先加入只有t点的边,然后次优先加入只有s的边,最后再加入既有s又有t的边。
③ds==dt.那么我们跑两遍即可,第一遍优先加入只有s点的边,然后次优先加入只有t的边,最后再加入既有s又有t的边。第二遍反过去,优先加入只有t点的边,然后次优先加入只有s的边,最后再加入既有s又有t的边。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
f[B]=A;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int s,t,ds,dt;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
scanf("%d%d%d%d",&s,&t,&ds,&dt);
int vals,valt;
if(ds>dt)vals=1,valt=2;
else vals=2,valt=1;
for(int i=0;i<m;i++)
{
a[i].use=0;
if(a[i].x==s||a[i].y==s)a[i].w=vals;
if(a[i].x==t||a[i].y==t)a[i].w=valt;
}
int cnt=0;
sort(a,a+m,cmp);
int dds=0,ddt=0;
for(int i=0;i<m;i++)
{
if(a[i].w==0)
{
if(find(a[i].x)!=find(a[i].y))
{
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
else
{
if(find(a[i].x)!=find(a[i].y))
{
if(a[i].x==s||a[i].y==s)
{
if(dds==ds)continue;
dds++;
}
if(a[i].x==t||a[i].y==t)
{
if(ddt==dt)continue;
ddt++;
}
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
}
if(dds<=ds&&ddt<=dt&&cnt==n-1)
{
printf("Yes\n");
for(int i=0;i<m;i++)
{
if(a[i].use==1)
printf("%d %d\n",a[i].x,a[i].y);
}
}
else //如果不行再反向跑一遍。
{
for(int i=1;i<=n;i++)f[i]=i;
int vals,valt;
if(ds>=dt)vals=1,valt=2;
else vals=2,valt=1;
for(int i=0;i<m;i++)
{
a[i].use=0;
if(a[i].x==s||a[i].y==s)a[i].w=vals;
if(a[i].x==t||a[i].y==t)a[i].w=valt;
}
int cnt=0;
sort(a,a+m,cmp);
int dds=0,ddt=0;
for(int i=0;i<m;i++)
{
if(a[i].w==0)
{
if(find(a[i].x)!=find(a[i].y))
{
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
else
{
if(find(a[i].x)!=find(a[i].y))
{
if(a[i].x==s||a[i].y==s)
{
if(dds==ds)continue;
dds++;
}
if(a[i].x==t||a[i].y==t)
{
if(ddt==dt)continue;
ddt++;
}
cnt++;
a[i].use=1;
merge(a[i].x,a[i].y);
}
}
}
if(dds<=ds&&ddt<=dt&&cnt==n-1)
{
printf("Yes\n");
for(int i=0;i<m;i++)
{
if(a[i].use==1)
printf("%d %d\n",a[i].x,a[i].y);
}
}
else
{
printf("No\n");
}
}
}
}
相关文章推荐
- Codeforces 723F st-Spanning Tree(连通性乱搞)
- CodeForces 732 E.Sockets(贪心)
- Codeforces 461A Appleman and Toastman(贪心)
- Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法
- Codeforces_484D:Kindergarten(贪心/DP)
- CodeForces - 839B Game of the Rows 贪心+思维
- CodeForces 492C Vanya and Exams(贪心)
- codeforces-#472B. Design Tutorial: Learn from Life(贪心)
- CodeForces 240D D. Merging Two Decks 解题报告 贪心
- codeforces_672C. Recycling Bottles(贪心)
- CodeForces 155B - Combination(贪心)
- HDOJ 4313 —— 树上的贪心类似kruskal
- codeforces 589F:贪心
- CodeForces - 808C-贪心-思维
- 【Codeforces 724 D Dense Subsequence】+ 贪心
- 算法训练 Beaver's Calculator/codeforces 207A1 排序+贪心
- CodeForces 398A Cards 贪心 暴力 瞎搞
- Codeforces 497C. Exams 贪心
- Codeforces 495C Treasure【贪心】
- CodeForces 718A Efim and Strange Grade (贪心)