Bzoj 1040: [zjoi2008]

Source: Internet
Author: User
1040: [zjoi2008] server guard time limit: 10 sec memory limit: 162 MB
Submit: 1755 solved: 690
[Submit] [Status] Description

The Knights of the Z state are a very influential organization, which brings together elites from all over the world. They have been praised by all walks of life for their part in the fight against wealth and poverty. A terrible thing happened recently. The evil country Y launched a war of aggression against the country Z. The war lasted for more than five hundred miles. in a peaceful environment, how did the Z-nation, who had been comfortable for hundreds of years, resist the army of country Y. As a result, people pin all their hopes on the knights, just like looking forward to the birth of a real dragon, and lead justice to defeat evil. The knights certainly have the ability to defeat evil forces, but the knights often have some conflicts with each other. Each server guard has only one of its most hated servers (not himself, of course). He will never seek out with the person he hates most. The war is spreading, and the people are suffering. It is imperative to organize a server Guard Corps to join the battle! The king handed you a tough task and elected a knight corps from all the knights, so that there is no conflict between the two in the Legion (there is no case that a knight and his most hated person are elected to the server Guard Corps), and this makes the server Guard Corps the most effective. In order to describe the combat power, we will give each knight an estimate of the combat power according to numbers 1 to n. the combat power of a legion is the total combat power of all the knights.

Input

The first line contains a positive integer N, which describes the number of knights. In the next n rows, each line contains two positive integers, which describe the combat power of each server and the server he hates most.

Output

A line should contain an integer, indicating the combat power of the server Guard Corps you selected.

Sample input3
10 2
20 3
30 1 sample output30hint

 

For 100% of the test data, n ≤ 000, each server guard is a positive integer not greater.

 

Source

I don't fully understand what the internal direction of an external website says on the Internet. To put it bluntly, this topic is a set of dynamic DP, since each person has only one limit, the most efficient structure will only be a series of fast connections that contain one limit, in these cases, do not set up additional DP.

This problem is caused by the fact that DP is very simple, and the attacker directly chooses to drop the last line, and then the two ends of this line are specially handled, so that it is not selected, and then DP on the slave.

This is the first time that I have used a replica set.

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<ctime>#include<cmath>#include<algorithm>#include<set>#include<map>#include<vector>#include<string>#include<queue>using namespace std;#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#define MAXN 1100000#define MAXV MAXN*2#define MAXE MAXV*2#define INF 0x3f3f3f3f#define INFL 0x00003f3f3f3f3f3fLLtypedef long long qword;inline int nextInt(){        char ch;        int x=0;        bool flag=false;        do                ch=getchar(),flag=(ch==‘-‘)?true:flag;        while(ch<‘0‘||ch>‘9‘);        do x=x*10+ch-‘0‘;        while (ch=getchar(),ch<=‘9‘ && ch>=‘0‘);        return x*(flag?-1:1);}int n,m;int e[MAXN][3];struct Edge{        int np;        Edge *next;}E[MAXE],*V[MAXV];int tope=-1;void addedge(int x,int y){        E[++tope].np=y;        E[tope].next=V[x];        V[x]=&E[tope];}int uf[MAXN];int get_fa(int now){        return (now==uf[now])?now:(uf[now]=get_fa(uf[now]));}bool comb(int x,int y){        x=get_fa(x);        y=get_fa(y);        if (x==y)return false;        uf[x]=y;        return true;}int roof[MAXN],topr=-1;int state[MAXN];int ptr[MAXN];int v[MAXN];pair<qword,qword> dfs(int now,int f){        pair<qword,qword> ret,t;        Edge *ne;        ret=make_pair(v[now],0);        for (ne=V[now];ne;ne=ne->next)        {                if (ne->np==f)continue;                t=dfs(ne->np,now);                ret.second+=max(t.first,t.second);                ret.first+=t.second;        }        if (state[now])ret.first=-INFL;        return ret;}int main(){        int i,j,k;        int x,y,z;        qword ans=0;        scanf("%d",&n);        memset(ptr,-1,sizeof(ptr));        for (i=0;i<=n;i++)uf[i]=i;        for (i=0;i<n;i++)        {                scanf("%d%d",&v[i+1],&e[i][1]);                e[i][0]=i+1;                if (!comb(e[i][0],e[i][1]))                {                        roof[++topr]=e[i][0];                        e[i][2]=true;                        ptr[e[i][0]]=e[i][1];                }else                {                        addedge(e[i][0],e[i][1]);                        addedge(e[i][1],e[i][0]);                }        }        for (i=0;i<=topr;i++)        {                state[get_fa(roof[i])]=true;        }        for (i=1;i<=n;i++)        {                if (!state[get_fa(i)])                {                        state[get_fa(i)]=true;                        roof[++topr]=get_fa(i);                }        }        memset(state,0,sizeof(state));        pair<qword,qword> pr;        qword t;        for (i=0;i<=topr;i++)        {                if (ptr[roof[i]]==-1)                {                        pr=dfs(roof[i],roof[i]);                        ans+=max(pr.first,pr.second);                }else                {                        //state[roof[i]]=true;                        state[ptr[roof[i]]]=true;                        pr=dfs(roof[i],roof[i]);                        t=max(pr.first,pr.second);                        state[ptr[roof[i]]]=false;                        pr=dfs(roof[i],roof[j]);                        t=max(t,pr.second);                        ans+=t;                }        }        printf(LL"\n",ans);        return 0;}

 

 

Bzoj 1040: [zjoi2008]

Related Keywords:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.