题意:给你一张无向图,要求对这张图进行删边操作,要求删边之后的图的总边数 >= ceil((n + m) / 2), 每个点的度数 >= ceil(deg[i] / 2)。(deg[i]是原图中i的度数)

思路1:模拟 + 乱搞

直接暴力删就行了,读入边之后随机打乱一下就很难被hack了。

代码:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define db double
#define pii pair<int, int>
using namespace std;
const int maxn = 1000010;
struct node {
int u, v, id;
};
vector<node> G;
int deg[maxn], limit[maxn], a[maxn];
pii b[maxn];
const LL mod = 1e9 + 7;
LL add(LL x, LL y) {return (x + y) % mod;}
LL mul(LL x, LL y) {return (x * y) % mod;}
bool vis[maxn];
bool cmp(int x, int y) {
return deg[x] > deg[y];
}
int main() {
int n, m, u, v;
scanf("%d%d", &n, &m);
int tot_limit = (n + m + 1) / 2;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
// G[u].push_back((node){u, v, i});
// G[v].push_back((node){v, u, i});
G.push_back((node){u, v, i});
deg[u]++;
deg[v]++;
}
for (int i = 1; i <= n; i++) {
limit[i] = (deg[i] + 1) / 2;
}
random_shuffle(G.begin(), G.end());
int ans = m;
for (int j = 0; j < G.size() && ans > tot_limit; j++) {
int v = G[j].v, now = G[j].u;
if(deg[v] == limit[v]) continue;
if(deg[now] == limit[now]) continue;
vis[j] = 1;
ans--;
deg[v]--;
deg[now]--;
}
printf("%d\n", ans);
for (int i = 0; i < m; i++) {
if(vis[i]) continue;
printf("%d %d\n", G[i].u, G[i].v);
}
}

思路2(官方题解):新建0号点,把0号点和图中所有度数为奇数的点相连,形成一张新图。在新图上跑一遍欧拉回路,把欧拉回路记录的边中偶数位置的删掉,删的时候如果是新加的边,就直接删了。否则,看一下这条边相邻的两条边是不是新加的边并且可以删,如果可以,那就删新加的边,否则删这条边。即迫不得已的情况才会删除原图的边。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
struct edge {
int u, v, flag;
}; int st[maxn * 2], ans[maxn * 2], re[maxn * 2];
edge a[maxn * 2];
int head[maxn], id[maxn * 4], Next[maxn * 4], ver[maxn * 4], tot, totm, tot_ans;
bool v[maxn * 4], vis[maxn * 4];
int deg[maxn];
int Top;
void add(int x, int y, int z) {
ver[++tot] = y, id[tot] = z, Next[tot] = head[x], head[x] = tot;
}
void euler (int s) {
tot_ans = 0;
st[++Top] = s;
while(Top > 0) {
int x = st[Top], i = head[x];
while(i && v[i]) i = Next[i];
if(i) {
st[++Top] = ver[i];
re[Top] = id[i];
v[i] = v[i ^ 1] = 1;
head[x] = Next[i];
} else {
ans[++tot_ans] = re[Top];
Top--;
}
}
}
int main() {
int n, m, x, y;
scanf("%d%d", &n, &m);
tot = 1;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
totm++;
a[totm] = (edge){x, y, 1};
add(x, y, totm), add(y, x, totm);
deg[x]++, deg[y]++;
}
for (int i = 1; i <= n; i++) {
if(deg[i] & 1) {
totm++;
a[totm] = (edge){0, i, 0};
add(0, i, totm), add(i, 0, totm);
}
}
int res = m;
for (int i = 0; i <= n; i++) {
euler(i);
for (int j = 2; j <= tot_ans; j += 2) {
int now = ans[j];
if(a[now].flag == 0) vis[now] = 1;
else {
int tmp = ans[j - 1];
if(a[tmp].flag == 0 && vis[tmp] == 0) {
vis[tmp] = 1;
continue;
}
int Next = j + 1;
if(j == tot_ans) Next = 1;
tmp = ans[Next];
if(a[tmp].flag == 0 && vis[tmp] == 0) {
vis[tmp] = 1;
continue;
}
vis[now] = 1;
res--;
}
}
}
printf("%d\n", res);
for (int i = 1; i <= totm; i++) {
if(vis[i] == 0) {
if(a[i].flag == 1) {
printf("%d %d\n", a[i].u, a[i].v);
}
}
}
}
//6 6
//3 4
//4 5
//5 3
//1 3
//1 2
//2 3

Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路的更多相关文章

  1. @codeforces - 1186F@ Vus the Cossack and a Graph

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图(n, m<=10^6),记第 ...

  2. Codeforces F. Vus the Cossack and Numbers(贪心)

    题目描述: D. Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. It is known that the ...

  3. codeforces 1186C Vus the Cossack and Strings

    题目链接:https://codeforc.es/contest/1186/problem/C 题目大意:xxxxx(自认为讲不清.for instance) 例如:a="01100010& ...

  4. codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)

    题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...

  5. Codeforces 193E - Fibonacci Number(打表找规律+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...

  6. Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题

    题意: 给你一个长度为 nnn 的 010101串 ,你有两种操作: 1.将一个子串翻转,花费 XXX 2.将一个子串中的0变成1,1变成0,花费 YYY 求你将这个01串变成全是1的串的最少花费. ...

  7. CodeForces - 1186 C. Vus the Cossack and Strings (异或)

    Vus the Cossack has two binary strings, that is, strings that consist only of "0" and &quo ...

  8. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  9. Codeforces Round #571 (Div. 2)-D. Vus the Cossack and Numbers

    Vus the Cossack has nn real numbers aiai. It is known that the sum of all numbers is equal to 00. He ...

随机推荐

  1. bzoj5047 [Lydsy1709月赛]空间传送装置 最短路

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5047 题解 题目中没有说可以停留在一个点等待.问了别人才知道停留是可以的. 那么既然停留是可以 ...

  2. C#高级编程(32章)ADO.net

    32.2高效地使用连接 sqlConnection类是针对sql的,而OleDbConnection 是针对其他比如access的,另外还有odbcConnection是针对odbc的,sql的访问优 ...

  3. centos 6.5 配置阿里云 yum 镜像

    配置国内镜像目的是为了加速软件下载安装速度,参考链接:http://mirrors.aliyun.com/help/centos 备份.养成文件操作前备份的习惯 cd /etc/yum.repos.d ...

  4. 以IP地址的形式访问网站

    一致以来我都习惯于用域名的方式来访问网站,看到同事用IP地址来访问网站,也仅仅只是好奇,试了一下也没成功,于是就放弃了. 最近发布了站点,客户并没有提供域名,于是通过IP地址访问的方法又在我的脑子里蹦 ...

  5. axios拦截器的使用方法

    很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...

  6. 【加密】RSA验签及加密

    通过OpenSSL生成公私钥文件(如果没有OpenSSL工具建议下载Cmder工具自带OpenSSL指令) 1.生成RSA密钥的方法 genrsa -out private-rsa.key 2048 ...

  7. Python爬虫之抓图

    从"百度图片(http://image.baidu.com/)"的首页下载图片 # -*- coding: utf-8 -*- import urllib import re im ...

  8. html插入链接的实现方法

    下面就为大家带来一篇html插入链接的实现方法.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   每个网页都已一个地址,用URL()标识,通常在网站内部创建链接时,通常 ...

  9. Delphi ListView的用法

    //增加 i := ListView1.Items.Count; with ListView1 do begin ListItem:=Items.Add; ListItem.Caption:= Int ...

  10. 剑指offer---4、序列化二叉树

    剑指offer---4.序列化二叉树 一.总结 一句话总结: 1. 对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点不为空时,在转化val所得的字符之后添加一个' , ' ...