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]表示第 ...
随机推荐
- Long类型数据传递到前端数据精度丢失问题
在开发页面的时候,遇到Long类型的数据,传送给前端遇到精度丢失的问题, 后端发的数据是这个. 前端接收到的数据是这样 解决的途径有二种:1 .在后端把Long类型的数据改成String类型(不推荐) ...
- 什么是Cassandra数据库
在本文中,我们将介绍Cassandra名字的含义.Cassandra的发展简史.Cassandra这项技术的特点及优势,以及对于这项技术的未来展望. 本文将用浅显易懂的方式,帮助您将对Cassandr ...
- 初探JAVA内部类细节一
定义: 可以将一个类的定义放在另一个类的内部 这就是内部类.--摘自java编程思想 一般实现方式: public class SimpleInnerClass { class Content { p ...
- ElasticSearch教程——自定义分词器(转学习使用)
一.分词器 Elasticsearch中,内置了很多分词器(analyzers),例如standard(标准分词器).english(英文分词)和chinese(中文分词),默认是standard. ...
- phoenix启动报错:org.apache.phoenix.exception.PhoenixIOException: SYSTEM.CATALOG
错误: org.apache.phoenix.exception.PhoenixIOException: SYSTEM.CATALOG at org.apache.phoenix.util.Serve ...
- MySQL中in('5,6,7')只取第一个id为5对应的数据的思考
通过阅读本文你可以更好的理解两个知识点: 1.#{}与${}在实际项目中的使用,避免在项目中使用不当造成不可预知的Bug; 2.MySQL中in里面如果是字符串的话,为什么只取第一个对应的数据,eg: ...
- 使用Pr压缩视频,增大音量
视屏压缩(亲测有效教程):https://www.zhihu.com/question/67762625 音量增大(亲测有效教程):https://jingyan.baidu.com/article/ ...
- 【渲染引擎】Blender的2021年最佳渲染引擎(上)
Blender最终摆脱了"古怪的孩子"的装束,并穿上了更为严肃和受人尊敬的" 3D强者". 它已在业界获得广泛认可,许多工作室和艺术家正在将其纳入他们的产品线. ...
- Jmeter(三十五) - 从入门到精通进阶篇 - 关联(详解教程)
1.简介 上一篇中介绍了如果想要同时发送多条请求,那么怎样才能让每条数据某些请求参数改变呢.这就用到了jMeter参数化.在实际测试场景中,我们往往还有这样的需求,登录后服务器响应的token作为下次 ...
- Haproxy-1.8.20 根据路径(URI)转发到后端不同集群
HAProxy根据不同的URI 转发到后端的服务器组 1 ) 实验内容说明: 1.1 ) 根据不同的URI 转发到后端的服务器组. /a /b 和其他 默认使用其他. 1.2 ) 使用IP介绍: ha ...