题意  给定一个$n$个点$n$条边的无向图,现在要把这个图进行若干次操作,并选择一个点作为首都。

   要求除首都外的任意两个点$u$, $v$,从$u$走到$v$必须经过这个首都。

操作为合并两个相邻的点为一个点,即把这两个点从原图中删除,连接这两个点的边接到新的点上去。

考虑最后这个图的形态其实是一个菊花图,那么可以考虑到最后剩下的这些点其实只有选出的首都和

原图中度数为$1$的点。

但是有这么一种比较特殊的情况。

这个图也是符合题意的。

原来的图其实是一个环套树(环的大小可能为$2$)

如果这个环上存在一个度数为$2$的点(即除了和环上的点相连之外其他没有点和他相连)

那么这个点也可以被留下,但是所有这样的点中最多只能留下一个。

于是答案就很显然了。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair typedef long long LL;
typedef pair <int, int> PII; const int N = 105; int vis[N], father[N], isroot[N], a[N];
int cnt;
int flag;
int n, now, ans;
int xx, yy;
map <PII, int> mp;
vector <int> v[N]; int get_circle(int x){
vis[x] = 1;
for (auto u : v[x]){
if (u == father[x]) continue;
father[u] = x;
if (vis[u]){
cnt = 0;
int w = x;
while (w ^ u){
a[++cnt] = w;
isroot[w] = cnt;
w = father[w];
} a[++cnt] = u;
isroot[u] = cnt;
return 1;
} if (get_circle(u)) return 1;
} return 0;
} void dfs(int x){
vis[x] = 1;
for (auto u : v[x]){
if (vis[u] || isroot[u]) continue;
dfs(u);
}
} class SuccessfulMerger{
public:
int minimumMergers(vector<int> road){
memset(a, 0, sizeof a);
memset(isroot, 0, sizeof isroot);
memset(father, 0, sizeof father);
memset(vis, 0, sizeof vis);
cnt = 0;
n = 0;
flag = 0;
mp.clear();
rep(i, 0, 100) v[i].clear();
for (auto u : road){
++n;
++u;
int x = n, y = u;
if (x > y) swap(x, y);
if (mp.count(MP(x, y))){
flag = 1;
xx = x, yy = y;
continue;
} mp[MP(x, y)] = 1;
v[x].push_back(y);
v[y].push_back(x);
} if (flag){
cnt = 2;
a[1] = xx;
a[2] = yy;
} else get_circle(1); if (flag){
v[xx].push_back(yy);
v[yy].push_back(xx);
} ans = n - 1;
rep(i, 1, n) if ((int)v[i].size() == 1) --ans; rep(i, 1, cnt){
int ccc = 0;
for (auto u : v[a[i]]){
int fg = 0;
rep(j, 1, cnt) if (u == a[j]) fg = 1;
if (fg == 0) ccc = 1;
} if (ccc == 0){ --ans; break; }
} return ans;
}
};

  

TopCoder SRM 682 Div1 Problem 450 SuccessfulMerger (环套树 + 分类讨论)的更多相关文章

  1. TopCoder SRM 675 Div1 Problem 500 LimitedMemorySeries1(分块)

    题意  给定一个长度不超过$5*10^{6}$的数列和不超过$100$个询问,每次询问这个数列第$k$小的数,返回所有询问的和 内存限制很小,小到不能存下这个数列.(数列以种子的形式给出) 时限$10 ...

  2. TopCoder SRM 722 Div1 Problem 600 DominoTiling(简单插头DP)

    题意  给定一个$12*12$的矩阵,每个元素是'.'或'X'.现在要求$1*2$的骨牌铺满整个矩阵, 'X'处不能放置骨牌.求方案数. 这道题其实和 Uva11270 是差不多的,就是加了一些条件. ...

  3. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  4. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  5. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. topcoder srm 685 div1

    problem1 link 依次枚举每个元素$x$,作为$S$中开始选择的第一个元素.对于当前$S$中任意两个元素$i,j$,若$T[i][j]$不在$S$中,则将其加入$S$,然后继续扩展:若所有的 ...

  8. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  9. Topcoder SRM 605 div1 题解

    日常打卡- Easy(250pts): 题目大意:你有n种汉堡包(统统吃掉-),每一种汉堡包有一个type值和一个taste值,你现在要吃掉若干个汉堡包,使得它们taste的总和*(不同的type值的 ...

随机推荐

  1. Codeforces Round #461 (Div. 2) B. Magic Forest

    B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  2. 二叉树的镜像(Python实现)

    题目 给定一棵二叉树,要求输出其左右翻转后二叉树的中序遍历. 例: 翻转前: 翻转后: 1 | 1 / \ | / \ 2 3 | 3 2 / \ | / \ 4 5 | 5 4 解析 两个步骤: 镜 ...

  3. centos使用--vsftpd配置

    目录 1 在服务器配置FTP服务 1.1 在root权限下,通过如下命令安装Vsftp(以CentOS系统为例): 1.2 在启动vsftpd服务之前,需要登录云服务器修改配置文件,禁用匿名登录. 1 ...

  4. 配置网络策略中的 NAP 条件

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 Win ...

  5. leetcode 【 Trapping Rain Water 】python 实现

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  6. MOCTF 简单注入

    最近在练习sql注入写脚本,记录一下思路,刚学的and 1=1也拿出来溜溜 http://119.23.73.3:5004/?id=1 首先,没有被过滤是正常显示. 没有被过滤但是查询不到就是空白,比 ...

  7. maven文件报错(pom.xml或者jar包缺失)解决方法

    相信很多朋友在myeclipse上把maven配置好了,但是新建maven项目的时候会报错,下面我来总结以下我遇到的问题. 新建完maven项目后,pom.xml报错 1.报错的原因:很多时候我们在下 ...

  8. Android之Bitmap 高效加载

    一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数 图片格式(Bitmap.Config) 一张100*100的图片占用内存的大小 ALPHA_8 图片长度*图片宽度 100 ...

  9. CodeForces875C[拓扑排序] Codeforces Round #440 [Div2E/Div1C]

    只要保存每相邻两行字符串 第一个不同位 即可.然后按照 第一个不同位上的字符有: " 来自下一行的 大于 来自上一行的" 构图,跑拓扑排序即可. 当然要判断一下有没有环构成, 有环 ...

  10. TypeError: $.ajaxFileUpload(…) is not a function

    今天做一个图片上传功能,用到了ajaxFileUpload,控制台报错TypeError: $.ajaxFileUpload(…) is not a function,都说是jQuery版本问题,也试 ...