题目链接: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的更多相关文章

  1. Codeforces #369 div2 D.Directed Roads

    D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

  7. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  8. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  9. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

随机推荐

  1. ExtJs、Struts2、Hibernate3.2登录页面的简单实现

    1.思想的大致模型 2.建立数据库test和数据库表tb_user 1 CREATEDATABASE `test`; 2  CREATETABLE `test`.`tb_user` ( 3 `user ...

  2. C# httprequest post 内容有百分号,部分特殊字符乱码问题

    哎没办法,还没完全脱离.net,已经一半了. http://stackoverflow.com/questions/7908581/how-to-encode-http-post-parameters ...

  3. 获取访问者ip的方法

    package com.mi.util; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.S ...

  4. 修改yv12像素值

    YV12内存布局 1 unsigned char *pY = (unsigned char *)pBuf; unsigned char *pV = pY + nWidth * nHeight; uns ...

  5. 移动前端中viewport(视口) 转

    移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dpi ( device-independent pixel 设备逻辑像素 )和 CSS 像素之间 ...

  6. UBI系统原理-中【转】

    转自:http://blog.chinaunix.net/uid-28236237-id-4217118.html UBI 文件头位置 EC头都放置在擦除块的开始偏移位置,占用64字节空间.之后防止V ...

  7. android手电筒(摇一摇也可开启手电筒)

    package com.firefly.myflashlight; import android.app.Activity; import android.hardware.Camera; impor ...

  8. HDU 5686:2016"百度之星" - 资格赛 Problem B

    原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others)    ...

  9. Shell基本功能&配置&Oh-My-Zsh

    http://blog.csdn.net/yangcs2009/article/details/45720193

  10. JavaScript编码风格指南(中文版)

    前言: 程序语言的编码风格对于一个长期维护的软件非常重要,特别是在团队协作中.如果一个团队使用统一规范的编码分风格,可以提高团队的协作水平和工作效率.编程风格指南的核心是基本的格式化规则,这些规则决定 ...