题解 CF1439B 【Graph Subset Problem】
搞崩
解析
不难发现度数小于
于是就将这些点删除;注意删除时可能还会产生新的度数小于
现在剩下的点的度数均不小于
考虑随便选一个度数恰好为 vector 存下每个结点的边,然后排序后用二分查找。这样验证一次的复杂度是
如果发现
考虑枚举团的总复杂度。对于
具体实现时,我们可以考虑直接删所有度数小于
这样做仍然是正确的。证明只需考虑我们删的每一个度数小于
CODE
实现 参考
实现常数较小的话这样就能过
再实在不行就上 hash 把 \fad(于是赛时一堆哈希被 hack...)
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using std::vector;
using std::queue;
using std::sort;
using std::binary_search;
/*------------------------------IO------------------------------*/
int read(){
int x =0; char c =getchar(); bool f =0;
while(c < '0' || c > '9') (c == '-') ? f =1, c =getchar() : c =getchar();
while(c >= '0' && c <= '9') x =(x<<1)+(x<<3)+(48^c), c =getchar();
return (f) ? -x : x;
}
void write(const int &x){
if(x/10)
write(x/10);
putchar('0'+x%10);
}
/*------------------------------Main------------------------------*/
int main(){
for(int t =0, T =read(); t < T; ++t){
int n =read(), m =read(), k =read();
vector<vector<int> > E(n);
for(int i =0; i < m; ++i){
int u =read()-1, v =read()-1;
E[u].push_back(v), E[v].push_back(u);
}
vector<int> d(n), vis(n)/*0 表示未访问,1 表示访问,2 表示删除*/;
for(int i =0; i < n; ++i){
d[i] =E[i].size();
sort(E[i].begin(), E[i].end());
}
queue<int> que;
for(int i =0; i < n; ++i)
if(d[i] < k){
que.push(i);
vis[i] =1;
}
vector<int> cilque;
while(!que.empty() && cilque.empty()){
int u =que.front();
que.pop();
vis[u] =2;
if(1ll*(k-1)*k/2 <= m && d[u] == k-1 && cilque.empty()){
cilque.push_back(u);
for(int v:E[u])
if(vis[v] != 2)
cilque.push_back(v);
bool OK =1;
for(int v:cilque){
for(int w:cilque){
if(v == w)/*一条无向边仅判一次*/
break;
else if(!binary_search(E[v].begin(), E[v].end(), w)){
OK =0;
break;
}
}
if(!OK)
break;
}
if(!OK)
cilque.clear();
}
for(int v:E[u])
if(--d[v] == k-1 && vis[v] == 0){
vis[v] =1;
que.push(v);
}
}
if(!cilque.empty()){
putchar('2'), putchar('\n');
for(int u:cilque)
write(u+1), putchar(' ');
putchar('\n');
}
else if(count(vis.begin(), vis.end(), 0) > 0){
vector<int> ans;
for(int i =0; i < n; ++i)
if(vis[i] == 0)
ans.push_back(i);
putchar('1'), putchar(' '), write(ans.size()), putchar('\n');
for(int u:ans)
write(u+1), putchar(' ');
putchar('\n');
}
else
putchar('-'), putchar('1'), putchar('\n');
}
}