Wizard's Tour CodeForces - 860D (图,构造)
大意: 给定$n$节点$m$条边无向图, 不保证连通, 求选出最多邻接边, 每条边最多选一次.
上界为$\lfloor\frac{m}{2}\rfloor$, $dfs$贪心划分显然可以达到上界.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m, cnt, vis[N];
vector<int> g[N];
struct {int x,y,z;} f[N];
void add(int x, int y, int z) {
f[++cnt]={x,y,z};
}
int dfs(int x) {
vis[x] = ++*vis;
int lst = 0;
for (int y:g[x]) {
if (!vis[y]) {
int t = dfs(y);
if (t) add(x,y,t);
else if (lst) add(lst,x,y),lst=0;
else lst = y;
}
else if (vis[y]>vis[x]) {
if (lst) add(lst,x,y),lst=0;
else lst = y;
}
}
return lst;
}
int main() {
scanf("%d%d", &n, &m);
REP(i,1,m) {
int u=rd(),v=rd();
g[u].pb(v),g[v].pb(u);
}
REP(i,1,n) if (!vis[i]) dfs(i);
printf("%d\n",cnt);
REP(i,1,cnt) printf("%d %d %d\n",f[i].x,f[i].y,f[i].z);
}
Wizard's Tour CodeForces - 860D (图,构造)的更多相关文章
- 【Codeforces858F】Wizard's Tour [构造]
Wizard's Tour Time Limit: 50 Sec Memory Limit: 512 MB Description Input Output Sample Input 4 5 1 2 ...
- Wizard's Tour
F. Wizard's Tour time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 1041 e 构造
Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...
- CodeForces 860D Wizard's Tour
题意 给出一张无向图,要求找出尽量多的长度为2的不同路径(边不可以重复使用,点可以重复使用) 分析 yzy:这是原题 http://www.lydsy.com/JudgeOnline/problem. ...
- Codeforces 1082 毛毛虫图构造&最大权闭合子图
A #include<bits/stdc++.h> using namespace std; typedef long long ll; , MAXM = ; //int to[MAXM ...
- Codeforces 976 正方格蛇形走位 二维偏序包含区间 度数图构造 贪心心火牧最大dmg
A #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; int main() { i ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- Codeforces.578E.Walking(构造)
题目链接 \(Description\) 给定一个长为\(n\)的足迹序列(只包含\(L,R\)两种字符),你需要\(LRLRLR...\)这样交替在\(L\)和\(R\)上走(第一步可以选择\(L\ ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
随机推荐
- HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)
题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m. 分析:这个子序列中只要大于等于m的个数大于等于k个即可.那么,我们可以用尺取法写,代码不难写,但是有些小细节需要 ...
- LeetCode---Bit Manipulation && Design
**401. Binary Watch 思路:产生两个list分别代表小时和分钟,然后遍历 public List<String> readBinaryWatch(int num) { L ...
- flask入门第一篇
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- (十四)C语言之一维数组、二维数组
- SRS之接收推流线程:recv
SrsPublishRecvThread.SrsRecvThread.SrsReusableThread2.SrsThread 之间的关系图 1. recv 线程函数:SrsThread::threa ...
- window7上爬虫框架Scrapy的安装 --错误分析lxml
本文讲解的是python已经安装成功的情况下,且pip可以使用 版本:python2.7.10 #安装 pip install Scrapy 在安装Scrapy框架时总会报lxml安装不了的各种错误, ...
- MAMP 环境下安装Redis扩展
1. 下载对应版本的 php 源码,(当前使用版本是 php5.6.30 )版本号最后一位有点区别不影响链接:http://php.net/get/php-5.6.31.tar.bz2/from/a/ ...
- find 命令详细解释
命令格式:find pathname -options [-print -exec -ok ...] 命令参数: pathname: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系 ...
- EXCEL中自定义格式输入的数据怎么完整复制
在用设置单元格式里 自定义 输入数值 如图,B列的数据,我复制后,用选择性粘贴到别的地方,还是无法将75FG4Y2一起复制过去,只能复制过去FG 怎么办? ===>先把这些复制到一个记事本里,再 ...
- Hibernate3核心API-SchemaExport类