P3119 [USACO15JAN]草鉴定Grass Cownoisseur
題目描述
In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X.
Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once).
As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ's paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.
約翰有n塊草場,編號1到n,這些草場由若干條單行道相連。奶牛貝西是美味牧草的鑒賞家,她想到達(dá)盡可能多的草場去品嘗牧草。
貝西總是從1號草場出發(fā),最后回到1號草場。她想經(jīng)過盡可能多的草場,貝西在通一個(gè)草場只吃一次草,所以一個(gè)草場可以經(jīng)過多次。因?yàn)椴輬鍪菃涡械肋B接,這給貝西的品鑒工作帶來了很大的不便,貝西想偷偷逆向行走一次,但最多只能有一次逆行。問,貝西最多能吃到多少個(gè)草場的牧草。
輸入輸出格式
輸入格式:INPUT: (file grass.in)
The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000).
The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.
輸入:
第一行:草場數(shù)n,道路數(shù)m。
以下m行,每行x和y表明有x到y(tǒng)的單向邊,不會(huì)有重復(fù)的道路出現(xiàn)。
輸出格式:OUTPUT: (file grass.out)
A single line indicating the maximum number of distinct fields Bessie
can visit along a route starting and ending at field 1, given that she can
follow at most one path along this route in the wrong direction.
輸出:
一個(gè)數(shù),逆行一次最多可以走幾個(gè)草場。
輸入輸出樣例
輸入樣例#1:? 7 10 1 2 3 1 2 5 2 4 3 7 3 5 3 6 6 5 7 2 4 7 輸出樣例#1:? 6說明
SOLUTION NOTES:
Here is an ASCII drawing of the sample input:
v---3-->6 7 | \ | ^\ v \| | \ 1 | | | v | v 5 4<--2---^Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling
backwards on the path between 5 and 3. When she arrives at 3 she
cannot reach 6 without following another backwards path.
?
solution:
本題Tarjan縮點(diǎn)+分層圖最短路。
一個(gè)環(huán)上的點(diǎn)顯然都是能互相到達(dá)的,所以我們先縮點(diǎn),并記錄每個(gè)強(qiáng)聯(lián)通分量的點(diǎn)個(gè)數(shù),對于逆行1次的情況,只需要建分層圖就好了,由于縮點(diǎn)后的圖是DAG,于是我們就可以拓?fù)渑判蛉ソ鉀Q第二問,當(dāng)然也可以建邊跑最長路咯。
代碼:
/*Code by 520 -- 9.5*/ #include<bits/stdc++.h> #define il inline #define ll long long #define RE register #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--) using namespace std; const int N=500005; int n,m,bl[N],scc,dfn[N],low[N],tot,stk[N],top; int to[N],net[N],h[N],cnt,w[N],dis[N],siz[N],f[N]; bool ins[N],vis[N]; struct node{int u,v; }e[N];int gi(){int a=0;char x=getchar();while(x<'0'||x>'9')x=getchar();while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+(x^48),x=getchar();return a; }il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;}void tarjan(int u){dfn[u]=low[u]=++tot,stk[++top]=u,ins[u]=1;for(RE int i=h[u];i;i=net[i])if(!dfn[to[i]]) tarjan(to[i]),low[u]=min(low[u],low[to[i]]);else if(ins[to[i]]) low[u]=min(low[u],dfn[to[i]]);if(dfn[u]==low[u]){++scc;while(stk[top+1]!=u)bl[stk[top]]=scc,siz[scc]++,ins[stk[top--]]=0;} }queue<int>q; int main(){n=gi(),m=gi();For(i,1,m) e[i].u=gi(),e[i].v=gi(),add(e[i].u,e[i].v);For(i,1,n) if(!dfn[i]) tarjan(i);memset(h,0,sizeof(h)),cnt=0;For(i,1,m) if(bl[e[i].u]!=bl[e[i].v]) {add(bl[e[i].u],bl[e[i].v]),w[cnt]=siz[bl[e[i].u]];add(bl[e[i].u]+scc,bl[e[i].v]+scc),w[cnt]=siz[bl[e[i].u]];add(bl[e[i].v],bl[e[i].u]+scc),w[cnt]=siz[bl[e[i].v]];}memset(dis,128,sizeof(dis));q.push(bl[1]),dis[bl[1]]=0;while(!q.empty()){RE int u=q.front();q.pop();vis[u]=0;for(RE int i=h[u];i;i=net[i])if(dis[to[i]]<dis[u]+w[i]){dis[to[i]]=dis[u]+w[i];if(!vis[to[i]])vis[to[i]]=1,q.push(to[i]);}}cout<<dis[bl[1]+scc];return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/five20/p/9595333.html
總結(jié)
以上是生活随笔為你收集整理的P3119 [USACO15JAN]草鉴定Grass Cownoisseur的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装Conda并在Conda下安装jup
- 下一篇: Raspbian安装Opencv3