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]表示第 ...
随机推荐
- 深入浅出!阿里P7架构师带你分析ArrayList集合源码,建议是先收藏再看!
ArrayList简介 ArrayList 是 Java 集合框架中比较常用的数据结构了.ArrayList是可以动态增长和缩减的索引序列,内部封装了一个动态再分配的Object[]数组 这里我们可以 ...
- 深入理解CSS盒模型【转载】
下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种模型 JS如何设置获取盒模型对应的宽和高 实例题(根据盒模型解释边距重叠) BFC(边距重叠解决方案) 基 ...
- Lambda 表达式实例
public class Java8Tester {/*** 语法 lambda 表达式的语法格式如下: (parameters) -> expression 或 (parameters) -& ...
- 线程上下文类加载器(Context ClassLoader)
1.线程上下文类加载器是从jdk1.2开始引入的,类Thread中的getContextClassLoader()与setContextClassLoader(ClassLoader c1),分别用来 ...
- SpringSecurity配置文件
@EnableWebSecurity public class seccurityConfig extends WebSecurityConfigurerAdapter { @Override pro ...
- 在onelogin中使用OpenId Connect Implicit Flow
目录 简介 OpenId Implicit Flow 创建onelogin的配置 页面的运行和请求流程 关键代码 总结 简介 onelogin支持多种OpenId Connect的连接模式,上一篇文章 ...
- Mac上“您没有权限来打开应用程序”(Big Sur)
最近电脑更新了Macos的最新11版大苏尔 Big Sur.很快问题就出现了:安装某个软件的时候Key Gen打不开,提示您没有权限来打开应用程序,类似这样:https://zhuanlan.zhih ...
- centos 安装 部署 gitlab github
https://www.cnblogs.com/wenwei-blog/p/5861450.html 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 ...
- (二)数据源处理5-excel数据转换实战(上)
把excel_oper02.py 里面实现的:通过字典的方式获取所有excel数据.放进utils: ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ utils: def get_al ...
- 日常采坑:.NET Core SDK版本问题
1..NetCore SDK版本问题 .NetCore3.1 webapi 部署linux,遇到一个坑,开启的目录浏览功能失效,几番尝试发现是版本问题.本地sdk版本与linux安装的sdk版本不对应 ...