CodeForces - 1228D
Posted lesning
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1228D相关的知识,希望对你有一定的参考价值。
乍一看,嗯,图论题,不错;
结果,这尼玛是模拟????
传送链接:https://codeforces.com/contest/1228/problem/D
看了大佬的代码瞬间就明白了许多!!!
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<set> #define maxn 300300 using namespace std; int de[maxn]; vector<int>anc[100]; int n, m; int vis[maxn]; int dfn[maxn]; int flag = 0; set<pair<int, int>>ins; int main() { int be, en; scanf("%d %d", &n, &m); if (m == 0) { printf("-1 "); return 0; } for (int i = 0; i < m; i++) { scanf("%d %d", &be, &en); ins.insert({ be,en }); ins.insert({ en,be }); } int cnt = 0; for (int i = 1; i <= n; i++) { if (dfn[i]) continue; cnt++; for (int j = i; j<=n ; j++) { if (ins.find({ i,j }) == ins.end()) { dfn[j] = cnt; anc[cnt].push_back(j); } } } if (cnt != 3 || anc[1].size() * anc[2].size() + anc[2].size() * anc[3].size() + anc[3].size() *anc[1].size() != m) { printf("-1 "); return 0; } else { for (int i = 1; i <= 3; i++) { for (int j = i + 1; j <= 3; j++) { for (int k = 0; k < anc[i].size(); k++) { int x = anc[i][k]; for (int s = 0; s < anc[j].size(); s++) { int p = anc[j][s]; if (ins.find({ x, p }) == ins.end()) { printf("-1 "); return 0; } } } } } } for (int i = 1; i <= n; i++) { printf("%d ", dfn[i]); } printf(" "); return 0; }
以上是关于CodeForces - 1228D的主要内容,如果未能解决你的问题,请参考以下文章
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段