题意:给你n个点m无向条边。每个点是黑色或者白色的。m条边第一条边边权为2^m,第二条边边权为2^(m-1)....... 。在这个图上选择一些边连起来,使得满足:每个黑点连奇数条边,每个白点连偶数条边的条件。并且选择的边权和最小。

这是CometOJ DIV2直播上讲的一道题,大佬说的解法如下:

首先如果一个连通块的黑点个数如果是偶数,那么把黑点当做叶子节点做一下生成树,一定是满足要求的,所以如果连通块黑点个数是偶数,必然存在一个子图满足要求。而如果黑点个数是奇数,因为点的度数总和是偶数,所以必然不满足要求。按权值从大到小考虑每条边,如果去掉一条边,不改变连通性,那么这条边肯定能去掉,那么最后剩下的就是改变连通性的最小的边,也就是最小生成树上的边。因为上面的性质,可以得到,如果一条边两端的黑点个数都是偶数,那么这条边可以去掉。

所以就是先对原图求一遍最小生成森林,然后对于每棵树求出这棵树的黑点个数,然后枚举最小生成森林上的边看看是否能删去。删去条件就是删除这条边后两边的黑点个数都要是偶数,那么就可以对这棵树随便抓一个点作为树根做dfs,求出d[i]代表以i为根的子树的黑点个数,那么对于一条边(x,y),割去这条边之后就会变成 1,以y为根的子树 2,除去y子树的其他树。 保证这两部分黑点个数为偶数即可删去。

这里有一个小细节:一个数减去偶数后 它奇偶性不会改变,所以可以连续删。

细节看代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,m,sum[N],fa[N],d[N],w[N];
struct edge{ int x,y; bool used=; }e[N<<];
vector<int> G[N];
bool vis[N]; int getfa(int x) { return x==fa[x] ? x : fa[x]=getfa(fa[x]); } void Kruskal() {
for (int i=;i<=n;i++) fa[i]=i;
for (int i=;i<=m;i++) {
int x=getfa(e[i].x),y=getfa(e[i].y);
if (x==y) continue;
G[e[i].x].push_back(e[i].y);
G[e[i].y].push_back(e[i].x);
e[i].used=; fa[y]=fa[x];
}
} void dfs(int x) {
d[x]+=w[x]; vis[x]=;
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
if (vis[y]) continue;
dfs(y);
d[x]+=d[y];
}
} int main()
{
freopen("uprtoff.in","r",stdin);
freopen("uprtoff.out","w",stdout);
cin>>n>>m;
char s[N]; scanf("%s",s+);
for (int i=;i<=n;i++) w[i]=(s[i]=='B'); for (int i=;i<=m;i++) scanf("%d%d",&e[m-i+].x,&e[m-i+].y);
Kruskal(); for (int i=;i<=n;i++) if (!vis[i]) dfs(i);
for (int i=;i<=n;i++)
sum[getfa(i)]=max(sum[getfa(i)],d[i]);
for (int i=;i<=n;i++)
if (sum[i]%) { puts(""); return ; } for (int i=m;i;i--)
if (e[i].used) {
int t=min(d[e[i].x],d[e[i].y]);
if (t%!= || (sum[getfa(e[i].x)]-t)%!=) printf("%d ",m-i+);
}
return ;
}

CFgym100020 Problem J. Uprtof的更多相关文章

  1. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  2. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  3. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  4. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  5. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  6. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  7. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题

    Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...

  8. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...

  9. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

随机推荐

  1. Java script-数组与字符串方法

    数组: 1.concat() 功能:用于连接两个或多个数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 参数:concat(data1,data2,...);所有参数可选,要合并的数 ...

  2. 如何优雅地在React中处理事件响应&&React绑定onClick为什么要用箭头函数?

    React绑定onClick为什么要用箭头函数? https://segmentfault.com/q/1010000010918131 如何优雅地在React中处理事件响应 https://segm ...

  3. 【转载】POST/GET请求中RequestBody和RequestParam的应用场景

    原文链接:https://blog.csdn.net/justry_deng/article/details/80972817 原文链接:https://www.jianshu.com/p/49819 ...

  4. 手写Spring事务框架

    Spring事务基于AOP环绕通知和异常通知 编程事务 声明事务 Spring事务底层使用编程事务+AOP进行包装的   = 声明事务 AOP应用场景:  事务 权限 参数验证 什么是AOP技术 AO ...

  5. 【leetcode】984. String Without AAA or BBB

    题目如下: Given two integers A and B, return any string S such that: S has length A + B and contains exa ...

  6. Java实现按汉语拼音的排序

    public class sortByPinyin { public static void main(String[] args) { String[] arr = { "刘刘" ...

  7. 【多线程】无锁编程以及CAS

    无锁编程 / lock-free / 非阻塞同步 无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在没有线程被阻塞的情况下实现变量的同步,所以也叫非阻塞同步(Non-blocking Sy ...

  8. 前端每日实战:77# 视频演示如何用纯 CSS 创作旗帜飘扬的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qydvBm 可交互视频 此视频是可 ...

  9. 用MyEclipse将java文件转换成UML类图

    用MyEclipse将java文件转换成UML类图 参考: 用MyEclipse将java文件转换成UML类图 - 君临天下的博客 - CSDN博客  http://blog.csdn.net/dan ...

  10. kafka manager遇到的一些问题

    1.启动后第一个报错如下: [error] k.m.a.c.BrokerViewCacheActor - Failed to get broker metrics for BrokerIdentity ...