CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads
题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边。这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方向反转),使得这个图里没有环。
思路:感觉关键是,n个点n条边,且每个点的出度为1,所以图里一定没有复环。想要使图里没环,对于每个连通块(点数为i)里的环(如果有环 点数为j),只要不是全翻和全不翻都是满足题意的set,
一共满足题意得set 即为 2^(i-j) * (2^j-2)。所有的连通块方案相乘即为最后的方案数ans.
找到所有的连通块并且得到里面的环的点数:邻接表存图dfs遍历连通块,全局变量标记当前连通块的点数,设置num数组标记每个点被访问时的次序,当再次被访问到时,两次标号相减
即为环的点数。但是这样的样例:
4
2 1 1 1
搜出来的环数会是1,因为3和4搜到1的时候确实1已经被标记了。而且覆盖了前面的2。然后... ... 本来想着先过样例吧...就在所有找到的环数里取max,觉得一定不对,比如这样:
10
2 1 1 1 1 1 1 1 1 1
居然是对的。
然后最后的ans被莫名其妙的%mod爆int。
附AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 2000100
#define LL long long
using namespace std;
const LL mod = 1e9+7; struct Node {
int u, v;
int nxt;
}edge[maxn]; //边数 int tot;
LL ans;
int num[maxn];
int numCir;
LL pow2[maxn];
int cntt;
int head[maxn];
bool vis[maxn]; void init() {
memset(num, 0, sizeof(num));
ans = 1;
tot = 0;
numCir = 0;
memset(head, -1, sizeof(head));
pow2[0] = 1;
for (int i=1; i<=maxn; ++i) {
pow2[i] = (pow2[i-1]*2)%mod;
}
// memset(vis, 0, sizeof(vis));
} void addEdge(int u, int v) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
} void dfs(int id, int cnt) {
cntt++;
//cout << id << "@+++++" << cntt << endl;
for (int i=head[id]; i!=-1; i=edge[i].nxt) {
int v = edge[i].v;
if (!vis[v]) {
num[v] = cnt+1;
vis[v] = 1;
dfs(v, cnt+1);
}
else { ///找到环了
numCir = max(cnt + 1 - num[v], numCir);
}
}
///搜完了整个连通块
} int main() {
//freopen("in.cpp", "r", stdin);
int n;
while(~scanf("%d", &n)) {
init();
// build map
for (int i=1; i<=n; ++i) {
int temp;
scanf("%d", &temp);
addEdge(i, temp);
addEdge(temp, i);
}
memset(vis, 0, sizeof(vis)); for (int i=1; i<=n; ++i) {
//cout << i << "@\n";
if (vis[i]) continue;
vis[i] = 1;
num[i] = 1;
cntt = 0;
numCir = 0;
dfs(i, 1);
// cout << numCir << " " << cntt << endl;
if (numCir == 0) ans += pow2[cntt];
else ans = (ans * ((pow2[cntt-numCir]*(pow2[numCir]-2))%mod))%mod;
}
ans %= mod;
printf("%I64d\n", ans);
}
return 0;
}
CodeForces #369 div2 D Directed Roads DFS的更多相关文章
- Codeforces #369 div2 D.Directed Roads
D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
随机推荐
- 视觉中的深度学习方法CVPR 2012 Tutorial Deep Learning Methods for Vision
Deep Learning Methods for Vision CVPR 2012 Tutorial 9:00am-5:30pm, Sunday June 17th, Ballroom D (Fu ...
- PHP 5.4 on CentOS/RHEL 6.4 and 5.9 via Yum
PHP 5.4 on CentOS/RHEL 6.4 and 5.9 via Yum PHP 5.4.16 has been released on PHP.net on 6th June 2013, ...
- bzoj1977 [BeiJing2010组队]次小生成树 Tree
和倍增法求lca差不多,维护每个点往上跳2^i步能到达的点,以及之间的边的最大值和次大值,先求出最小生成树,对于每个非树边枚举其端点在树上的路径的最大值,如果最大值和非树边权值一样则找次大值,然后维护 ...
- ECMAScript 6教程 (三) Class和Module(类和模块)
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文连接,博客地址为 http://www.cnblogs.com/jasonnode/ .该系列课程是 ...
- Unable to create SVNRepository object
I think you are missing at least the library setup stuff: /* * Initializes the library to w ...
- JavaWeb应用开发架构浅谈
本文就我所经历和使用过的技术和框架, 讨论 Java / Javascript 技术组合构成的Web 应用架构. 一. 概述 Web 应用架构可以划分为两大子系统:前端子系统和后台子系统. 前端子系统 ...
- 点击checkbox,触发事件
时间选择: 起始时间:<input type="text" value="2016-03-21 12:24:10" id="starttime& ...
- openlayers 学习笔记之1
1. 为Web Gis客户端开发的javascript 框架 百度文库中的教程:入门经典> 1) 初始化map: map = new OpenLayers.Map(mapContainerNam ...
- Unity的旋转-四元数,欧拉角用法简介
当初弄不明白旋转..居然找不到资料四元数应该用轴角相乘...后来自己摸明白了 通过两种旋转的配合,可以告别世界空间和本地空间矩阵转换了,大大提升效率. 每个轴相乘即可,可以任意轴,无限乘.无万向节锁问 ...
- 服务器端查看log的shell脚本
持续过滤log脚本 服务器端持续查看log的shell脚本(其中path1和path2替换为路径特征名,“tail -f”后面接的路径替换为路径特征名所对应的log文件路径): #! /bin/sh ...