Codeforces 1439B. Graph Subset Problem (思维,复杂度分析)
题意
给出一张无向图,让你找出一个大小为\(k\)的子团或者找出一个导出子图,使得图中的每个点的度数至少为\(k\)。
思路
首先有个重要观察,当\(\frac{k(k-1)}{2} > m\)时,无解,因为无论是满足要求子团还是导出子图至少有\(\frac{k(k-1)}{2}\)条边,于是我们把\(k\)降到了\(O(\sqrt{m})\)的级别。
对于导出子图,我们用类似拓扑序的方法一次将度数\(<k\)的点删去,剩下的点就是所求导出子图。当我们从队列中取出一个度数是\(k-1\)的点的时候,我们暴力判断这先连到的点是否是完全图,复杂度\(O(k^2)\)或\(O(k^2logn)\),取决于实现,每次我们判断一个这样的完全图,就会删掉这\(k-1\)条边,于是最多只会做\(\frac{m}{k-1}\)次,于是复杂度是\(O(\frac{m}{k-1}) \cdot O(k^2) = O(mk) = O(m\sqrt m)\)
代码
#include<bits/stdc++.h>
#define ll long long
#define N 100015
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
#define all(x) x.begin(),x.end()
using namespace std;
int t,n,m,k,in[N],vis[N],gkp[N];
queue<int> q;
VI e[N],cli;
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
while(!q.empty()) q.pop();
rep(i,1,n) e[i].clear(),in[i] = vis[i] = gkp[i] = 0;
rep(i,1,m){
int u,v; scanf("%d%d",&u,&v);
e[u].pb(v); e[v].pb(u);
in[u]++;in[v]++;
}
int tot = n;
if(k*(k-1) > 2*m) {puts("-1"); continue;}
rep(i,1,n) if(in[i] < k) q.push(i);
rep(i,1,n) sort(all(e[i]));
while(!q.empty()){
int u = q.front(); q.pop();
if(vis[u]) continue;
tot--;
vis[u] = 1;
if(in[u] == k-1){
cli.clear(); cli.pb(u);
for(auto x:e[u]){
if(vis[x]) continue;
cli.pb(x);
}
bool ff = 0;
for(auto p:cli){
for(auto q:cli){
if(p == q) continue;
if(!binary_search(all(e[p]),q)){
ff = 1; break;
}
}
}
if(ff == 0){
puts("2");
for(auto x:cli) printf("%d ",x);
printf("\n");
goto fuckyou;
}
}
vis[u] = 1;
for(auto v:e[u]){
in[v]--;
if(vis[v]) continue;
if(in[v] < k) q.push(v);
}
}
if(tot > 0){
printf("%d %d\n",1,tot);
rep(i,1,n) if(!vis[i]) printf("%d ",i);
printf("\n");
}else puts("-1");
fuckyou: continue;
}
return 0;
}
/*
1
2 1 2
1 2
*/
Codeforces 1439B. Graph Subset Problem (思维,复杂度分析)的更多相关文章
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- Codeforces 755E:PolandBall and White-Red graph(构造+思维)
http://codeforces.com/contest/755/problem/E 题意:给出n个点和一个距离d,让你在这个n个点的图里面构造一个子图,使得这个子图的直径和补图的直径的较小值为d, ...
- codeforces 807 D. Dynamic Problem Scoring(贪心+思维)
题目链接:http://codeforces.com/contest/807/problem/D 题意:对于动态计分的 Codeforces Round ,已知每题的 score 是根据 Round ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
- Codeforces 948C Producing Snow(优先队列+思维)
题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...
随机推荐
- JavaScript 获取数组对象中某一值封装为数组
1.获取数组对象中某一值封装为数组(一) data = [["2000-06-05",116],["2000-06-06",129]]; var dateLis ...
- Redis主从复制getshell技巧
Redis未授权漏洞常见的漏洞利用方式: Windows下,绝对路径写webshell .写入启动项. Linux下,绝对路径写webshell .公私钥认证获取root权限 .利用contrab计划 ...
- Java利用VLC开发简易视屏播放器
1.环境配置 (1)下载VLC VlC官网http://www.videolan.org/ 各个版本的下载地址http://download.videolan.org/pub/videolan ...
- TensorFlow 基础概念
初识TensorFlow,看了几天教程后有些无聊,决定写些东西,来夯实一下基础,提供些前进动力. 一.Session.run()和Tensor.eval()的区别: 最主要的区别就是可以使用sess. ...
- PHPstorm 配置主题
1.首先先去下载自己喜欢的主题:http://www.phpstorm-themes.com/ 但是在下载的时候会发现一个问题,在点击下载后,并没有下载,而是会打开这个文件(不同的浏览器不同)但是如果 ...
- 【.NET与树莓派】上手前的一些准备工作
.NET Iot 不是什么新鲜事物,百科很强大,故老周在此也不必多介绍.现在的时代和老周当年学 QBasic 的时代不同,那时候拉根电话线上网,下载速度只有可怜的 3.5 kb/s.而且还要去店里买上 ...
- Unity优化 1
浅谈Unity中的GC以及优化(转) Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译文搬运了过来,作为上面思 ...
- 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...
- LeetCode682 棒球比赛
题目描述: 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. "+"(一轮的得分):表示本 ...
- LeetCode-151-中等-翻转字符串里面的单词
问题描述 给定一个字符串,逐个翻转字符串中的每个单词. 说明: 无空格字符构成一个 单词 . 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 如果两个单词间有多余的空格,将反转 ...