您的位置:首页 > 其它

UVALive 6897 Exploration(逆向思维)

2015-12-15 01:23 225 查看
题意:

给定一个N≤2000的图,求一个最大团使得团内每个点的度数至少为k≤2000

分析:

赛上正着想了半天这个签到题,最后发现倒着一想,不停的删掉不符合的点最后剩下的不就是满足的最大团了么

一个队列类似拓扑排序搞搞就好了

代码:

//
//  Created by TaoSama on 2015-12-14
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 2e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m, k;
vector<int> G
;

bool vis
;
int deg
;

int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

int t; scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &k, &m);
for(int i = 1; i <= n; ++i) G[i].clear();
memset(deg, 0, sizeof deg);
memset(vis, false, sizeof vis);
while(m--) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
++deg[u], ++deg[v];
}

int ans = n;
queue<int> q;
for(int i = 1; i <= n; ++i)
if(deg[i] < k) q.push(i), vis[i] = true, --ans;
while(q.size()) {
int u = q.front(); q.pop();
for(int v : G[u]) {
if(--deg[v] < k && !vis[v]) {
--ans;
vis[v] = true;
q.push(v);
}
}
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  思维