【链接】点击打开链接


【题意】


给你n个点m条边的无权无向联通图;
让你找3个点A,B,C
使得A->B=B->C=A->C
这里X->Y表示点X到点Y的最短路长度.

【题解】


考虑一个出度大于等于3的点x.
任取其3个出度a,b,c
如果a和b有边相连,则输出x,a,b->一个长度为3的环
如果a和c有边相连,则输出x,a,c
如果b和c有边相连,则输出x,b,c
上面三种情况都排除了,则直接输出a,b,c
显然,它们互相之间的最短路都为2,因为上面的判断已经把最短路为1的情况排除掉了.
如果没有出度大于等于3的点x的话?
则所有的点度数都小于等于2.(无向图)
这个时候肯定是一条链或者是一个环.
只有边数和点数相同的时候才是一个环.
环的大小为3的倍数才是一个合法的能够找到答案的环.

【错的次数】


1

【反思】


一开始只想到了环的情况,后面想到了那种特殊的答案,但苦于不知道怎么表示。
也没能细想。

【代码】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <cstdlib>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ld long double
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rf(x) scnaf("%lf",&x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
#define sz(x) ((int) x.size())
#define ld long double typedef pair<int, int> pii;
typedef pair<LL, LL> pll; //mt19937 myrand(time(0));
//int get_rand(int n){return myrand()%n + 1;}
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5; int n, m;
vector <int> g[N + 10];
map <int, bool> have[N + 10];
vector <int> v;
bool flag[N + 10]; void dfs(int x) {
if (flag[x]) return;
v.pb(x);
flag[x] = true;
int len = sz(g[x]);
rep1(i, 0, len - 1) {
int y = g[x][i];
dfs(y);
}
} int main() {
//Open();
//Close();
ri(n), ri(m);
rep1(i, 1, m) {
int x, y;
ri(x), ri(y);
g[x].pb(y), g[y].pb(x);
have[x][y] = true,have[y][x] = true;
}
rep1(i, 1, n)
if (sz(g[i])>=3){
int x = g[i][0], y = g[i][1], z = g[i][2];
if (have[x][y])
return printf("%d %d %d\n", i, x, y),0;
if (have[x][z])
return printf("%d %d %d\n", i, x, z), 0;
if (have[y][z])
return printf("%d %d %d\n", i, y, z), 0;
return printf("%d %d %d\n", x, y, z), 0;
}
if (n % 3 == 0 && n == m) {
dfs(1);
printf("%d %d %d\n", v[0], v[n / 3], v[n / 3 + n / 3]);
}
else puts("-1");
return 0;
}

【CS Round #43 D】Bad Triplet的更多相关文章

  1. 【CS Round #43 E】Coprime Pairs

    [链接]点击打开链接 [题意] 让你选择n个数字,组成一个数组,使得这n个数字中恰好有k对,它们是互质的. [题解] 我们可以先找出前n个质数,那么接下来的问题就转化为,凑出rest = n*(n-1 ...

  2. 【CS Round #43 C】Rectangle Partition

    [链接]点击打开链接 [题意] 有一辆火车,它的长度为L,然后假设这辆车现在随机可能地出现在0..D之间,然后假设它已经耗光了油. 问你它需要走的期望距离是多少. 这里要走的距离指的是车里最近的加油站 ...

  3. 【CS Round #43 B】Rectangle Partition

    [链接]https://csacademy.com/contest/round-43/task/rectangle-partition/ [题意] 水题 [题解] 横着过去,把相邻的边的宽记录下来. ...

  4. 【CS Round #43 A】Expected Dice

    [链接]https://csacademy.com/contest/round-43/task/expected-dice/ [题意] 大水题 [题解] 把36种可能的结果都存下来. 然后把重复出现的 ...

  5. 【CS round 34】Minimize Max Diff

    [题目链接]:https://csacademy.com/contest/round-34/task/minimize-max-diff/ [题意] 给你n个数字; 数组按顺序不下降; 让你删掉k个数 ...

  6. 【CS Round 34】Max Or Subarray

    [题目链接]:https://csacademy.com/contest/round-34/summary/ [题意] 让你找一个最短的连续子串; 使得这个子串里面所有数字or起来最大; [题解] 对 ...

  7. 【CS Round #36 (Div. 2 only) A】Bicycle Rental

    [题目链接]:https://csacademy.com/contest/round-36/task/bicycle-rental/ [题意] 让你从n辆车中选一辆车; 每一辆车有3个属性 1.到达车 ...

  8. 【CS Round #37 (Div. 2 only) D】Reconstruct Graph

    [Link]:https://csacademy.com/contest/round-37/task/reconstruct-graph/statement/ [Description] 给你一张图; ...

  9. 【CS Round #37 (Div. 2 only) B】Group Split

    [Link]:https://csacademy.com/contest/round-37/task/group-split/ [Description] 让你把一个数分成两个数a.b的和; (a,b ...

随机推荐

  1. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  2. UML类图中的几种关系总结

           UML类图,描写叙述对象和类之间相互关系的方式包含:依赖(Dependency).关联(Association).聚合(Aggregation).组合(Composition).泛化(G ...

  3. hdu5372 Segment Game

    Problem Description Lillian is a clever girl so that she has lots of fans and often receives gifts f ...

  4. 什么时候用button,什么时候用a标签

    什么时候用button,什么时候用a标签 一.问题 能实现链接功能的标签一般就a标签,button标签,input submit标签 input submit肯定是提交表单的时候用 那什么时候用but ...

  5. 使用h5 <a>标签 href='url' download 下载踩过的坑

    用户点击下载多媒体文件(图片/视频等),最简单的方式: <a href='url' download="filename.ext">下载</a> 如果url ...

  6. Kinect 开发 —— 骨骼追踪 (下)

    基于景深数据的用户交互 骨骼数据中关节点不仅有X,Y值,还有一个深度值 除了使用WPF的3D特性外,在布局系统中可以根据深度值来设定可视化元素的尺寸大小来达到某种程序的立体效果. 下面的例子使用Can ...

  7. groupadd---创建一个新的工作组

    groupadd命令   groupadd命令用于创建一个新的工作组,新工作组的信息将被添加到系统文件中. 语法 groupadd(选项)(参数) 选项 -g:指定新建工作组的id: -r:创建系统工 ...

  8. C# 将引用的DLL文件放到指定的目录下

    原文:C# 将引用的DLL文件放到指定的目录下 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sweety820/article/details/2 ...

  9. JQuery之为某个div加入行样式

    JQuery都是以$符号开头的.当然能够用jQuery取代$符号,他们是恒等的,同一时候也是相等的.()事实上就是一个方法,里面能够传递匿名函数等,选取某个div时,如id为div1则用$('#div ...

  10. (二十二)unity4.6学习Ugui中文文档-------交互-Eventsystem &amp; Binding

    大家好,我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...