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]表示第 ...
随机推荐
- GraduateDesign-给APP添加获取位置信息和天气预报的功能(json)
首先,我的app需要通过网络来获取当前所在的位置.这里我找到了一个json来获取本地位置信息. http://int.dpool.sina.com.cn/iplookup/iplookup.php?f ...
- Python获取网页html代码
获取网页html代码: import requests res = requests.get('https://www.cnblogs.com/easyidea/p/10214559.html') r ...
- 2020年Python文章盘点,我选出了个人TOP10
大家好,我是猫哥.2020年过得真快啊!总感觉这一年里还没有做成多少事,一眨眼就又到了写年度总结的时候了-- 去年1月1日的时候,我写了<我的 2019 年 Python 文章榜单>,简单 ...
- mysql性能调优注意事项
1.最左原则 注意遇到> < like between失效 2.对于like 查询 遇到最左%索引无效 3.SQL性能优化目标:至少要达到range(对索引进行范围查找)级别,要求是 ...
- VScode中配置C++运行环境
目录 VScode中配置C++运行环境 1. 哪些插件 2. 配置开始 3. 编写代码并运行 VScode中配置C++运行环境 关于安装mingw的教程,网络上已经有很多了,这里不再赘述,下面就看VS ...
- SpringSecurity配置文件
@EnableWebSecurity public class seccurityConfig extends WebSecurityConfigurerAdapter { @Override pro ...
- 如何在Spring Boot项目中集成微信支付V3
Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 演示例子: paym ...
- Adnc简介
Adnc是一个轻量级的.Net Core微服务(microservices)快速开发框架,同时也可以应用于单体架构系统的开发.框架基于JWT认证授权,包含基础的后台管理功能,代码简洁.易上手.学习成本 ...
- ubutu14.04选中文本就会删除问题解决
1.网上解法 (1)重装ibus sudo apt-get install --reinstall ibus (2)键盘格局为english(united state) (3)升级火狐为最新版 以上方 ...
- Java基础--接口回调(接口 对象名 = new 类名)理解
接口 对象名1 = new 类名和类名 对象名2 = new 类名的区别是什么? 实例 /** *Person.java 接口 */ public interface Person { void in ...