两种解法:

一、树状DP

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int INF=;
const int mxn=;
int hd[mxn],to[mxn],next[mxn];
int f[mxn],g[mxn],c[mxn];
int vis[mxn];
int cnt=;
int n,p,eg;
void add_edge(int u,int v){
to[++cnt]=v;next[cnt]=hd[u];hd[u]=cnt;
return;
}
void dfs(int now){
vis[now]=;
if(vis[c[now]])
p=now;
else dfs(c[now]);
return;
}
void solve(int now,int fa){
f[now]=;g[now]=INF;vis[now]=;
if(now==eg){
g[now]=;
}
int u=hd[now];
while(u!=){
if(to[u]!=fa && to[u]!=p)
{
// printf("test msg4: now (%d) to (%d) \n",now,to[u]);
solve(to[u],now);
g[now]+=min(f[to[u]],g[to[u]]);
g[now]=min(g[now],f[now]+f[to[u]]-);
f[now]+=min(f[to[u]],g[to[u]]);
// printf("test msg5: f[now]: %d g[now]: %d \n",f[now],g[now]);
}
u=next[u];
}
}
int main(){
scanf("%d",&n);
int i,j;
for(i=;i<=n;i++){
scanf("%d",&c[i]);
add_edge(c[i],i);//反向存边
}
int ans=;
for(i=;i<=n;i++){
if(!vis[i]){
// printf("test msg1: dfs(%d)\n",i);
dfs(i);
// printf("test msg2: p(%d)\n",p);
eg=c[p];
solve(p,);
int tmp=f[p];
// printf("test msg3: tmp(%d)\n",tmp);
eg=;
solve(p,);
ans+=min(tmp,g[p]);
}
}
printf("%d\n",n-ans);
}

二、强行拓扑贪心

  AC

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int INF=;
const int mxn=;
int in[mxn];
int ctl[mxn];
bool flag[mxn];
int n,cnt=;
int ans=;
int main(){
scanf("%d",&n);
int i,j;
for(i=;i<=n;i++){
scanf("%d",&ctl[i]);
in[ctl[i]]++;//统计入度
}
queue<int>q;
for(i=;i<=n;i++){
if(!in[i]) q.push(i);
}
int tmp;
while(!q.empty()){
tmp=q.front();
q.pop();
if(!flag[tmp] && !flag[ctl[tmp]]){
ans++;
flag[ctl[tmp]]=;
in[ctl[ctl[tmp]]]--;
if(!in[ctl[ctl[tmp]]]){//减后入度为0
q.push(ctl[ctl[tmp]]);
} }
flag[tmp]=;
}
for(i=;i<=n;i++){
if(!flag[i]){
cnt=;j=i;
flag[i]=;
while(ctl[j]!=i){
flag[ctl[j]]=;//处理环
cnt++;
j=ctl[j];
}
ans+=cnt/;//环上一半的点可以投放
}
}
printf("%d\n",ans);
return ;
}

bzoj3037 创世纪的更多相关文章

  1. BZOJ3037 创世纪[基环树DP]

    实际上基环树DP的名字是假的.. 这个限制关系可以看成每个点有一条出边,所以就是一个内向基环树森林. 找出每个基环树的环,然后对于树的部分,做DP,设状态选或不选为$f_{x,0/1}$,则 $f_{ ...

  2. BZOJ3037 创世纪(基环树DP)

    基环树DP,攻的当受的儿子,f表选,g表不选.并查集维护攻受关系.若有环则记录,DP受的后把它当祖宗,再DP攻的. #include <cstdio> #include <iostr ...

  3. 【BZOJ3037/2068】创世纪/[Poi2004]SZP 树形DP

    [BZOJ3037]创世纪 Description applepi手里有一本书<创世纪>,里面记录了这样一个故事……上帝手中有着N 种被称作“世界元素”的东西,现在他要把它们中的一部分投放 ...

  4. [bzoj3037/2068]创世纪[Poi2004]SZP_树形dp_并查集_基环树

    创世纪 SZP bzoj-3037/2068 Poi-2004 题目大意:给你n个物品,每个物品可以且仅可以控制一个物品.问:选取一些物品,使得对于任意的一个被选取的物品来讲,都存在一个没有被选取的物 ...

  5. Poetize4 创世纪

    3037: 创世纪 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 123  Solved: 66[Submit][Status] Description ...

  6. 为创世纪图书馆(Library Genesis)作镜像

    简介 Library Genesis的Wikipedia条目中的介绍是: Library Genesis or LibGen is a search engine for articles and b ...

  7. 编程哲学之C#篇:01——创世纪

    我们能否像神一样地创建一个世界? 对于创建世界而言,程序员的创作能力最接近于神--相对于导演,作家,漫画家而言,他们创建的世界(作品)一旦完成,就再也不会变化,创建的角色再也不会成长.而程序员创建的世 ...

  8. CH6401 创世纪

    6401 创世纪 0x60「图论」例题 描述 上帝手中有 N(N≤10^6) 种世界元素,每种元素可以限制另外1种元素,把第 i 种世界元素能够限制的那种世界元素记为 A[i].现在,上帝要把它们中的 ...

  9. 图形学创世纪——写在SIGGRAPH 40年的边上

    40年的边上" title="图形学创世纪--写在SIGGRAPH 40年的边上"> 前言: SIGGRAPH是由ACM SIGGRAPH(美国计算机协会计算机图形 ...

随机推荐

  1. openstack虚拟机迁移的操作记录

    需求说明:计算节点linux-node1.openstack:192.168.1.8  计算节点linux-node2.openstack:192.168.1.17 这两个计算节点在同一个控制节点下( ...

  2. python中的Unittest常用方法

    import unittest class SimpleUnitTest(unittest.TestCase): def test_Fail(self): self.failUnless(True) ...

  3. U3D assetbundle加载

    using UnityEngine; using System.Collections; public class testLoadFromAB : MonoBehaviour { IEnumerat ...

  4. GIS开源软件大全

    3 - F 3map:行星地球项目由3map驱动,这是一个自由软件,由Telstra宽带基金会创建并支持,提供客户端与服务器的能力以在线再现虚拟地球. Amein!:其界面介于ArcMap和UMN M ...

  5. Eclipse中10个最有用的快捷键组合(转)

    Eclipse中10个最有用的快捷键组合 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask ...

  6. opencv中的图像复制、保存和显示

    接下来几天会写一个opencv的基础系列,与各位相互学习! &1 图像操作 声明图像指针:IplImage* 读入图像: cvLoadImage 创建图像:cvCreateImage 复制图像 ...

  7. C++创建对象的两种方式

    C++创建对象有两种方式,在栈上创建对象(Objects on the Stack)和在堆上创建对象(Objects on the Heap). 假设我们有以下的类: #include <str ...

  8. GSON

    { "data": [ { "children": [ { "id": 10007, "title": "北京 ...

  9. [CareerCup] 14.4 Templates Java模板

    14.4 Explain the difference between templates in C++ and generics in Java. 在Java中,泛式编程Generic Progra ...

  10. 如何下载struts 2及其各个包的作用

    一.http://archive.apache.org/dist/struts/library/ 二. struts官网: http://struts.apache.org/ 进入主页后点击" ...