• bzoj1040 [ZJOI2008]骑士


    Description

      Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英。他们劫富济贫,惩恶扬善,受到社会各界的赞扬。最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争。战火绵延五百里,在和平环境中安逸了数百年的Z国又怎能抵挡的住Y国的军队。于是人们把所有的希望都寄托在了骑士团的身上,就像期待有一个真龙天子的降生,带领正义打败邪恶。骑士团是肯定具有打败邪恶势力的能力的,但是骑士们互相之间往往有一些矛盾。每个骑士都有且仅有一个自己最厌恶的骑士(当然不是他自己),他是绝对不会与自己最厌恶的人一同出征的。战火绵延,人民生灵涂炭,组织起一个骑士军团加入战斗刻不容缓!国王交给了你一个艰巨的任务,从所有的骑士中选出一个骑士军团,使得军团内没有矛盾的两人(不存在一个骑士与他最痛恨的人一同被选入骑士军团的情况),并且,使得这支骑士军团最具有战斗力。为了描述战斗力,我们将骑士按照1至N编号,给每名骑士一个战斗力的估计,一个军团的战斗力为所有骑士的战斗力总和。

    Input

      第一行包含一个正整数N,描述骑士团的人数。接下来N行,每行两个正整数,按顺序描述每一名骑士的战斗力和他最痛恨的骑士。

    Output

      应包含一行,包含一个整数,表示你所选出的骑士军团的战斗力。

    Sample Input

    3
    10 2
    20 3
    30 1

    Sample Output

    30

    HINT

    N ≤ 1 000 000,每名骑士的战斗力都是不大于 1 000 000的正整数。

    正解:强联通分量+树形dp。

    显然虽然给的边指定了方向,但其实是无向图。找到一个强联通分量以后随便断一条边,跑两遍树的最大独立集,分别不包括断边两端的点就行了。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <cstdio>
     7 #include <vector>
     8 #include <cmath>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 #include <set>
    13 #define inf 1<<30
    14 #define il inline
    15 #define RG register
    16 #define ll long long
    17  
    18 using namespace std;
    19  
    20 int head[1000010],vis[1000010],vi[1000010],a[1000010],t[1000010],d[1000010],n,num,cnt;
    21 ll f[1000010],g[1000010],ans;
    22  
    23 struct edge{ int nt,to; }G[2000010];
    24  
    25 il int gi(){
    26     RG int x=0,q=0; RG char ch=getchar();
    27     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
    28     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    29 }
    30  
    31 il void insert(RG int from,RG int to){ G[++num]=(edge){head[from],to},head[from]=num; return; }
    32  
    33 il void dfs(RG int x,RG int p){
    34     vis[x]=1,vi[x]=cnt,f[x]=(ll)a[x],g[x]=0;
    35     for (RG int i=head[x];i;i=G[i].nt){
    36     RG int v=G[i].to; if (vi[v]==cnt) continue; dfs(v,p);
    37     f[x]+=g[v]; if (v!=p) g[x]+=max(f[v],g[v]); else g[x]+=g[v];
    38     }
    39     return;
    40 }
    41  
    42 il void work(){
    43     n=gi(); for (RG int i=1;i<=n;++i) a[i]=gi(),t[i]=gi(),d[t[i]]++;
    44     for (RG int i=1;i<=n;++i){ RG int x=i; while (!vis[x] && !d[x]) insert(x,t[x]),insert(t[x],x),vis[x]=1,x=t[x],d[x]--; }
    45     for (RG int i=1;i<=n;++i){
    46     RG int last=0,x=i; while (!vis[x]){ if (last) insert(last,x),insert(x,last); vis[x]=1,last=x,x=t[x]; }
    47     if (last){
    48         RG ll res1,res2; ++cnt; dfs(x,last); res1=max(f[x],g[x]);
    49         ++cnt; dfs(last,x); res2=max(f[last],g[last]),ans+=max(res1,res2);
    50     }
    51     }
    52     printf("%lld
    ",ans); return;
    53 }
    54  
    55 int main(){
    56     work();
    57     return 0;
    58 }
  • 相关阅读:
    英雄大乱斗
    深浅拷贝(copy)
    myleecode
    代码量简单统计
    pandas 模块 05
    matplotlib 模块 07
    KVC 和KVO浅谈
    iOS开发中懒加载的使用和限制
    关于空白模板插件的使用
    UIImageC处理
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416567.html
Copyright © 2020-2023  润新知