Topcoder srm div2 500

原创 2016年06月01日 22:07:00

Problem Statement

  Hero has just constructed a very specific graph. He started with n isolated vertices, labeled 0 through n-1. For each vertex i Hero then chose a vertex a[i] (other than i) and he added an edge that connected i and a[i]. This way he created a graph with n vertices and n edges. Note that if a[x]=y and a[y]=x, the vertices x and y were connected by two different edges. Hero now wants to perform the following procedure:
  1. Add a new isolated vertex number n.
  2. Choose a subset M of the original vertices.
  3. For each x in M, erase an edge between vertices x and a[x].
  4. For each x in M, add a new edge between vertices x and n.
Hero's goal is to create a final graph in which the vertices 0 through n-1 are all in the same connected component. (I.e., there must be a way to reach any of these vertices from any other of them by following one or more consecutive edges, possibly visiting vertex n along the way.) Note that Hero does not care whether vertex n is in the same component as the other vertices: both possibilities are fine. In step 2 of the above procedure Hero has 2^n possible subsets to choose from. A choice of M is good if it produces a graph with the desired property. Count how many of the 2^n possibilities are good choices. Return that count as a long long.

题意是说给出一个数组a,a[i]=j表示i到j有一条边。然后选定一个点的集合,这个集合中所有点a[x]=y的边都去掉,并把这些点都连接向点n。如果最终形成的图还是联通,那么就是好集合。问这样的集合有多少个。

和CF22E很像。。。如果每一个联通分量里面点的个数为x,尾巴上的点的个数为y,那么这块的集合的个数为2^x-2^y。把每一部分乘到一起。

代码:

#pragma warning(disable:4996)
#include <fstream>
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define eps 1e-12
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define pper(i,n,m) for(int i = n;i >= m; i--)
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back

const int maxn = 500005, maxd = 18;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);

ll vis[maxn], cnt[maxn];
set<int>u;

class Sunnygraphs2
{
public:
	long long count(vector <int> a)
	{
		ll res = 1;
		int sz = a.size();
		int uni = 1;
		int i, j, k;
		
		u.clear();
		u.insert(0);

		mem(cnt, 0);
		mem(vis, -1);
		for (i = 0; i < sz; i++)
		{
			ll now = 1;
			int x = i;
			if (vis[x] == -1)
			{
				while (vis[x] == -1)
				{
					vis[x] = i;
					now <<= 1;
					cnt[x] = now;
					x = a[x];
				}
				if (u.find(vis[x]) != u.end())
				{
					u.insert(i);
				}
				else
				{
					uni = 0;
				}
				if (vis[x] == i)
				{
					res = res*(now - (cnt[x] >> 1));
				}
				else
				{
					res = res*now;
				}
			}
		}
		if (uni == 1)
			res++;
		return res;
	}
};




版权声明:本文为博主原创文章,未经博主允许不得转载。

topcoder 乱作系列1 srm500 srm501 srm502

SRM 500 250pts答案一定是被选次数最多的人被选的概率,模拟第一次后的结果,考虑剩下的个数。以后每次剩下的都是用n%当前的个数。#include using namespace std; ...
  • make_it_for_good
  • make_it_for_good
  • 2016年08月24日 18:59
  • 503

topcoder SRM 548 DIV2 500

虽说好像有插件,但是不会用,不知道怎么调试,只好到外面进行了,用于测试的完整代码如下 #include #include using namespace std; int minLevel(vect...
  • u010262426
  • u010262426
  • 2013年11月28日 13:21
  • 458

topcoder SRM 500 div2 level3

Problem Statement   NOTE: This problem statement contains superscripts that may not display...
  • ascii991
  • ascii991
  • 2012年05月19日 10:41
  • 350

[JZOJ4844]抗拒黄泉/[Topcoder SRM583 Hard]

题目大意给定一个n×mn\times m的01矩阵,每次随机选择任意一个为11的格子并将其标记(只是标记,没有改变数值)。现在我们要使整个矩阵每一行、每一列都至少有一个格子被标记。求出期望的步数。1≤...
  • a_crazy_czy
  • a_crazy_czy
  • 2016年11月02日 17:09
  • 607

Topcoder SRM 582 DIV2 500

Problem Statement   Magical Girls are girls who have magical powers. They fight against evi...
  • jjike
  • jjike
  • 2013年06月18日 20:29
  • 684

TopCoder SRM(Arena) 一日游记

呃,其实我是个菜如一只jie
  • fcxxzux
  • fcxxzux
  • 2014年06月26日 14:59
  • 6819

Topcoder SRM523-527(DIV2)

.
  • qq_35392050
  • qq_35392050
  • 2017年01月20日 22:06
  • 196

Topcoder:SRM 708 算法题解

题目翻译 250分题目:SafeBetting 赌徒有b块钱,他想把自己手上的钱增加到c块,同时他又不想输的太惨,因此必须保证每次下注后手上不少于a块钱。每次下注,赢了则下注的钱按双倍奉...
  • Hans__Yang
  • Hans__Yang
  • 2017年02月11日 11:21
  • 677

TopCoder SRM 669 DIV 1

250 Problem Statement   The idols Ami and Mami like playing games. Today they bought a new...
  • solotzg
  • solotzg
  • 2015年10月03日 21:52
  • 900

TopCoder SRM569 题解

这场考得跟什么一样的……rating怒跌300+…… 翻译向。官方题解:http://apps.topcoder.com/wiki/display/tc/SRM+569 250pts: ...
  • huzecong
  • huzecong
  • 2013年02月17日 20:53
  • 1411
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Topcoder srm div2 500
举报原因:
原因补充:

(最多只允许输入30个字)