Gym - 101149I: It's the Police (图,思维)
题目链接:https://vjudge.net/problem/Gym-101149I
先找一个与其他广场连接最少的广场作为窝点,然后再让与其相连的广场不站警察即可
#pragma GCC optimize(2) #include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mst(a) memset(a, 0, sizeof(a)) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; const double eps = 1e-7; const int INF = 0x3f3f3f3f; const ll ll_INF = 0x3f3f3f3f3f3f3f; const int maxn = 2e5+10; vector<int> G[maxn]; int res[maxn]; int main(void) { int n, m; scanf("%d%d", &n, &m); fill(res, res+maxn, 1); //默认全都能占满 if (!m) { //如果没有街道连接,就随便选一个广场不站人 res[1] = 0; for (int i = 1; i<=n; ++i) printf(i == n ? "%d\n" : "%d ", res[i]); return 0; } for (int i = 0, a, b; i<m; ++i) { //存图 scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } int _min = INF; for (int i = 1; i<=n; ++i) //选出来与其他广场连接最少的广场 _min = min(_min, G[i].size()); for (int i = 1; i<=n; ++i) if (G[i].size() == _min) { //让选出来的广场作为窝点,与之相连的广场不站人 res[i] = 0; for (int j = 0; j<_min; ++j) res[G[i][j]] = 0; break; } for (int i = 1; i<=n; ++i) printf(i == n ? "%d\n" : "%d ", res[i]); return 0; }