Time limit 1000 ms

Memory limit 262144 kB

这题是一场cf里,div1的第三题,div2的第5题

中文题意

给一张无向图,没说连通性,要你选出一个大小为n的匹配,或者大小为n的独立集。换句话说,要你选出n条互相没有公共点的边,要是没有就选出n个互相没有直接相邻的点。直接相邻:被一条给定边连接。

解题思路

官方题解

Let's try to take edges to matching greedily in some order. If we can add an edge to the matching (both endpoints are not covered), then we take it. It is easy to see that all vertices not covered by the matching form an independent set — otherwise we would add an edge to the matching. Either matching or independent set has size at least \(n\). Complexity — \(O(n+m)\).

我的想法

一开始看见觉得很玄学啊,但仔细一想还真是那么回事。是这么选:给了\(3n\)个点嘛,对于输入的每条边,如果两个端点都没被选择过,就把这条边附带这两个点选上,并把两个端点标记为用过。如果输入完,选择了足够数量的边(大于等于n),那就输出这些边就好。

如果边没选够,那么说明存在多于\(n\)个未使用的点(选一条边用掉2个点,而总共\(3n\)个点),这些点当中的每一个,它的邻居中已经没有未被使用的了。

因为如果一个点未被使用,且它的邻居也未被使用,那么之前选边的时候这条边就被选了,它也就被使用了,它和它的邻居就会被标记上被使用过。换句话说,这个未使用的点和其他未使用的点之间没有边相连,被孤立了。其他未使用的点也同理。

这剩下的至少\(n\)个点就是题目要的独立集啊。于是就输出这些点。

另外,如果选中的边数量正好等于\(n\),说明对应独立集的大小也是\(n\),这时候按题意随便输出一种就好。

cf上给这题打的构造、贪心、图论、排序的标签(排序?)。确实不需要排序吧……还是因为官方题解的第一句话——

Let's try to take edges to matching greedily in some order

然后有order这个词,就给它打上排序的标签?(突然杠精)

源代码

#include<stdio.h>
#include<string.h> int T;
int n,m;
bool used[300010];//用于标记点是否被使用
int e[500010],cnt;//用于记录选中的边的编号和数量,用于输出
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(used,0,(n+2)*3);
cnt=1;
for(int i=1,u,v;i<=m;i++)
{
scanf("%d%d",&u,&v);
if(!used[u]&&!used[v])
{
used[u]=used[v]=1;
e[cnt++]=i;
}
}
if(cnt>n)
{
puts("Matching");
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
}
else
{
int num=0;
puts("IndSet");
for(int i=1;i<=3*n&&num<n;i++)//写的时候脑袋不清晰,WA了好几次,这个循环条件改了好几次
{
if(used[i]) continue;
printf("%d ",i);
num++;
}
}
puts("");
}
return 0;
}

最新文章

  1. [python] CSV read and write using module xlrd and xlwt
  2. 转载:Chrome调试折腾记_(1)调试控制中心快捷键详解!!!
  3. js限制文本框只可以输入数字
  4. poj 1704 阶梯博弈
  5. c#结构体和字节数组的转换、字节数组和stream的转换
  6. MetInfo操作笔记
  7. BZOJ 4518 [Sdoi2016]征途(分治DP)
  8. Android 4.4 沉浸式透明状态栏与导航栏
  9. 谈谈那些年PHP中屌屌的验证码
  10. CSDN CODE平台,中国版Github简要使用说明
  11. 还原openstack配置文件的方法
  12. CodeForces 591A Wizards&#39; Duel
  13. mvn
  14. iOS网络模块优化(失败重发、缓存请求有网发送)
  15. confluence6.x安装
  16. Java 关于类的构造方法的一点认识
  17. BeanCreationException: Error creating bean with name &#39;transactionManager&#39; defined
  18. GO介绍,环境的配置和安装 简单使用
  19. 【Robot Framework 项目实战 01】使用 RequestsLibrary 进行接口测试
  20. 2018 徐州赛区网赛 G. Trace

热门文章

  1. zimg 服务器配置文件
  2. linux系统中不小心执行了rm -rf ./* 怎么办?解决:文件系统的备份与恢复
  3. [2019上海网络赛F题]Rhyme scheme
  4. Requests的基本使用
  5. dsu on tree 与长链剖分
  6. P2523 [HAOI2011]Problem c
  7. spark 在启动的时候出现JAVA_HOME not set
  8. thinkphp5 select对象怎么转数组?
  9. Java Annotation 刷课笔记(一)
  10. 10年前文章_fedora10root登录