图论 ---- B. Graph Subset Problem (图中找k阶完全子图 or 找一个子集里面的点的度数都打过k)

题目链接


题目大意:

在这里插入图片描述


解题思路:

输出1的解法:

  1. 就是我们每次都把 d e g r e e ( a ) < k degree(a)<k degree(a)<k的点删除,如果最后还剩下点那就可以输出 1 1 1

输出2的解法:

  1. 就是我们每次都把 d e g r e e ( a ) < k − 1 degree(a)<k-1 degree(a)<k1的删除,如果访问到了 d e g r e e = k − 1 degree=k-1 degree=k1的点,把那个 k − 1 k-1 k1个点 p u s h push push v e c t o r vector vector,然后 k ∗ k ∗ l o g k k*k*logk kklogk进行判断!!
  2. 我们建图用 v e c t o r vector vector进行建图,对每次点进行 s o r t sort sort
  3. 因为图的边数才 1 e 5 1e5 1e5,那么完全图最大才 s q r t ( 1 e 5 ) sqrt(1e5) sqrt(1e5)复杂度完全可以接受

AC code

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {
   x = 0;char ch = getchar();ll f = 1;
   while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
   while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
   read(first);
   read(args...);
}
vector<int> G[maxn], cilpue, res;
int d[maxn], vis[maxn];
int n, m, k;
inline bool check() {// 找解法2
	for(int i = 0; i < res.size(); ++ i)
	   for(int j = i + 1; j < res.size(); ++ j) {
		   int pos = lower_bound(G[res[i]].begin(),G[res[i]].end(),res[j]) - G[res[i]].begin();
		   if(pos == G[res[i]].size()) return 0;
		   if(G[res[i]][pos] != res[j]) return 0;
	   }
	return 1;
}

inline void init() {
	for(int i = 0; i <= n; ++ i) G[i].clear(), d[i] = 0, vis[i] = 0;
	res.clear(); 
	cilpue.clear();
}

int main() {
	IOS;
	int T;
	cin >> T;
	while(T --) {
		cin >> n >> m >> k;
		init();
		for(int i = 1; i <= m ;++ i) {
			int u, v;
			cin >> u >> v;
			G[u].push_back(v);
			G[v].push_back(u);
			d[u] ++;
			d[v] ++;
		}
		for(int i = 1; i <= n; ++ i) sort(G[i].begin(),G[i].end());// 对链接的点排序
		priority_queue<PII, vector<PII>, greater<PII> > q; // 用优先队列,重复插入不怕,毕竟会先访问到小的
		for(int i = 1; i <= n; ++ i) q.push({d[i],i});
		int is = 0;
		while(!q.empty()) {
			auto top = q.top();
		    if(top.first >= k) break;
			q.pop();
			if(vis[top.second]) continue;
			vis[top.second] = 1;
			bool flag = 0;
			if(d[top.second] == k - 1  && 1ll * k * (k - 1) <= 2ll * m) flag = 1, res.push_back(top.second); // 特判
			for(auto it : G[top.second]) {
				if(vis[it]) continue;
				if(flag) res.push_back(it);
				d[it] --;
				q.push({d[it],it});
			} 
			if(flag && check()) {
				is = 1;// 找到方法二就退出去了
				break;
			}
			res.clear();
		}
		if(!is) {
			for(int i = 1; i <= n; ++ i)
				if(!vis[i])
					cilpue.push_back(i);			
		}
		if(is) {
			cout << 2 << endl;
			for(auto it : res)
			  cout << it << " ";
			cout << endl;
		} else if(!cilpue.empty()) {
			cout << 1 << " " << cilpue.size() << endl;
			for(auto it : cilpue)
			   cout << it << " ";
			cout << endl;
		} else cout << "-1\n";
	} 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值