Always keep a beginner's mind, don't forget the |

Creeper_l

园龄:1年4个月粉丝:10关注:13

Partition into Groups 题解

原题链接:Partition into Groups

PS:这是今天上午NOIP模拟赛的T3。

题意

N个小朋友,每个小朋友最多有3个敌对小朋友,
问是否能把他们分成两组,使得这N个小朋友最多只有一个敌对小朋友在一组。

思路

考场上想肯定与二分图有关,最后没想出来,打了15分暴力就走了(最后还只有10分)

考后听完xqw大佬讲题过后,发现思路很巧妙。

首先将两组看作两个集合,初始时都在\(1\)集合。每一次将两个集合中不符合题目要求的点(即在当前集合中敌对数\(>=2\)的点)换到另外一个集合中去,重复执行此操作,最终一定会得到答案。

对没错,这道题一定有解,不可能输出NO SULOTION,这也就是此题奇妙之处了。

证明

首先每次将点换集合的条件是:在当前集合中敌对数\(>=2\)。而题目说过每个点最多一共有\(3\)个敌对点,所以:

换集合后,原来集合敌对数至少\(-2\),新集合敌对数至多\(+1\)

所以每次操作至少会减少一个敌对数,所以一直操作一定可以满足题意。

总结

这道题主要考的还是思维,证明的思路非常难想但又非常优美。

Code

#include<bits/stdc++.h>
using namespace std;
#define ls id << 1
#define rs id << 1 | 1
const int MAXN = 1e5 + 10;
int n,c[MAXN],x,cnt,head[MAXN],col[MAXN],q[MAXN],ans1[MAXN],ans2[MAXN],top1,top2;
bool vis[MAXN];
struct Node
{
	int u,v,nxt;
}e[MAXN << 4];
void Add(int u,int v){e[++cnt] = {u,v,head[u]};head[u] = cnt;}
signed main()
{
	memset(head,-1,sizeof head);
	scanf("%d",&n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%d",&c[i]);
		for(int j = 1;j <= c[i];j++)
		{
			scanf("%d",&x);
			Add(i,x);
		}
	}
	int l = 0,r = 0;
	for(int i = 1;i <= n;i++) col[i] = vis[i] = 1,q[r++] = i;
	for(;l != r;l++)
	{
		int u = q[l],sum = 0;
		vis[u] = false;
		for(int i = head[u]; ~ i;i = e[i].nxt)
		{
			int now = e[i].v;
			if(col[u] == col[now]) sum++;
		}
		if(sum >= 2)
		{
			col[u] = 3 - col[u];
			for(int i = head[u]; ~ i;i = e[i].nxt)
			{
				int now = e[i].v;
				if(vis[now] == false) if(r < 1e6) q[r++] = now;
			}
		}
	}
	for(int i = 1;i <= n;i++) 
	{
		if(col[i] == 1) ans1[++top1] = i;
		if(col[i] == 2) ans2[++top2] = i; 
	}
	if(top1 < top2 || (top1 == top2 && col[1] == 1))
	{
		printf("%d\n",top1);
		for(int i = 1;i <= top1;i++) printf("%d ",ans1[i]);
	}
	else 
	{
		printf("%d\n",top2);
		for(int i = 1;i <= top2;i++) printf("%d ",ans2[i]);
	}
    return 0;
}

本文作者:Creeper_l

本文链接:https://www.cnblogs.com/Creeperl/p/17913350.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Creeper_l  阅读(3)  评论(0编辑  收藏  举报
  1. 1 イエスタデイ(翻自 Official髭男dism) 茶泡饭,春茶,kobasolo
  2. 2 世间美好与你环环相扣 柏松
  3. 3 True love tired
  4. 4 一笑江湖 (DJ弹鼓版) 闻人听書_
  5. 5 最好的安排 曲婉婷
  6. 6 星星在唱歌 司南
  7. 7 山川 李荣浩
  8. 8 On My Way Alan Walker
  9. 9 百战成诗 王者荣耀·100英雄官方群像献礼歌
  10. 10 雪 Distance Capper / 罗言
  11. 11 Edamame bbno$ / Rich Brian
  12. 12 半生雪 七叔-叶泽浩
  13. 13 Catch My Breath Kelly Clarkson
  14. 14 Love Is Gone SLANDER / Dylan Matthew
  15. 15 Endless Summer Alan Walker / Zak Abel
  16. 16 悬溺 葛东琪
  17. 17 风吹丹顶鹤 葛东琪
  18. 18 Normal No More TYSM
  19. 19 哪里都是你 队长
  20. 20 Stronger Kelly Clarkson
  21. 21 廖俊涛
  22. 22 消愁 毛不易
  23. 23 The Runner Yubik
  24. 24 踏山河 七叔-叶泽浩
  25. 25 Waiting For Love Avicii
  26. 26 在你的身边 盛哲
  27. 27 Dream It Possible Delacey
  28. 28 凄美地 郭顶
  29. 29 满天星辰不及你 ycc
  30. 30 侧脸 于果
  31. 31 阿拉斯加海湾 蓝心羽
  32. 32 虞兮叹 闻人听書_
  33. 33 离别开出花 就是南方凯
  34. 34 盗墓笔记·十年人间 李常超 (Lao乾妈)
一笑江湖 (DJ弹鼓版) - 闻人听書_
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示