zoukankan      html  css  js  c++  java
  • CodeForces 1198C 1199E Matching vs Independent Set

    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;
    }
    
  • 相关阅读:
    &与&&的区别
    x^y=(x&~y)|(~x&y)证明
    a、b交换与比较
    x+y = ((x&y)<<1) + (x^y) 证明
    (x&y) + ((x^y)>>1)即x和y的算数平均值
    默认参数提升
    类型转换
    闲扯原码,补码和反码(转)
    C/C++中float和double的存储结构
    led设备驱动(s3c_led.c)
  • 原文地址:https://www.cnblogs.com/wawcac-blog/p/11288070.html
Copyright © 2011-2022 走看看