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.




#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];

class Sunnygraphs2
	long long count(vector <int> a)
		ll res = 1;
		int sz = a.size();
		int uni = 1;
		int i, j, k;

		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())
					uni = 0;
				if (vis[x] == i)
					res = res*(now - (cnt[x] >> 1));
					res = res*now;
		if (uni == 1)
		return res;



TOPCODER/SRM 566 DIVII(250、500、1000题)(1000PT暂未附上代码)

Problem Statement   Percy has just received a new game called Penguin Tiles. The game...

51nod 1179:最大的最大公约数

1179 最大的最大公约数 题目来源: SGU 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 ...

topcoder SRM500 DIV1 level2 500分

Problem Statement   Nick likes to draw fractals. For the special occasion of Single Round M...

TopCoder SRM 654 Div2 Problem 500 - OneEntrance (思维)


TopCoder SRM 653 Div2 Problem 500 - RockPaperScissorsMagicEasy (DP)


topcoder SRM 500 div2 level3

Problem Statement   NOTE: This problem statement contains superscripts that may not display...

Topcoder SRM 687 (Div 2) 500.Quacking __ string matches

Ducks have started mysteriously appearing in your room. All ducks make the same sound: "quack". Each...

Topcoder SRM 503 DIV1 500 KingdomXCitiesandVillages

KingdomXCitiesandVillagesTask:给定n个城市和m个村庄的坐标。要求将每一个村庄都连接到城市。 你每一次选定一个未连接的村庄,寻找这个村庄到其他一个已经连接到城市的村庄或者...

TopCoder SRM687 div1 500

题意是已知一个无向图所有点对间的最小割,构造一个合法的原图。   看了题解得知有个叫Gomory-Hu tree的东西,即最小割树。解题要点是,你要知道一个图的所有点对最小割,一定能用一棵树做到。于...