bzoj3887: [Usaco2015 jan]grass Cownoisseur

Source: Internet
Author: User

Test instructions

Give a directed graph and choose a path with a path starting point of 1, and one chance to go in the opposite direction of a certain edge, and ask how many points can be passed? (a point in the path, regardless of how many positive integer number of contributions to the answer are 1)

= = Map We will consider the point of contraction first. Then observe the graph after the indentation to find that the new path must have only one edge reversed to meet the criteria. Then we can think of some of the shortest-circuiting problem will be the side of the opposite to save one after another run from S and T. So here BFS run a run on the line. Then there is a pit point: the attention of this reconstruction diagram es and edges or ES will be modified in the middle of the ...

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <queue > #include <stack>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s ; i>=t;i--) #define CLR (x,c) memset (x,c,sizeof (x)) #define QWQ (x) for (Edge *o=head[x];o;o=o->next) #define QAQ (x) For (Edge *o=hd[x];o;o=o->next) #define TAT (x) for (edge *o=eo[x];o;o=o->next) int read () {int X=0;char c=getchar (); while (!isdigit (c)) C=getchar (), while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int Nmax=1e5+5;const int inf=0x7f7f7f7f;struct edge{int to;edge *next;}; Edge es[nmax],edges[nmax<<1],*pt=es,*head[nmax],*hd[nmax],*eo[nmax];void Add (int u,int v) {pt->to=v;pt- >next=head[u];head[u]=pt++;} void Adde (int u,int v) {pt->to=v;pt->next=hd[u];hd[u]=pt++;p t->to=u;pt->next=eo[v];eo[v]=pt++;} int pre[nmax],dfs_clock=0,scc_cnt=0,sccno[nmax],sm[nmax];stack<int>s;int dfs (int x) {int Lowu=pre[x]=++dfs_ Clock;s.push (x); QWQ (x) {if (!pre[o->to]) lowu=min (Lowu,dfs (o->to)), else if (!sccno[o->to]) lowu=min (Lowu,pre[o->to]);} if (Lowu==pre[x]) {++scc_cnt;int Tc=0;while (1) {int tx=s.top (); S.pop (); sccno[tx]=scc_cnt;++tc;if (X==TX) break;} SM[SCC_CNT]=TC;} return LOWU;} Queue<int>q;bool vis[nmax];int f[nmax],g[nmax];int test_cnt=0;void bfs1 (int x) {q.push (x); F[x]=sm[x];int tx;clr (vis,0); while (!q.empty ()) {Tx=q.front (); Q.pop (); Vis[tx]=0;qaq (TX) if (F[o->to]<f[tx]+sm[o->to]) {f[o-> To]=f[tx]+sm[o->to];if (!vis[o->to]) Q.push (o->to), Vis[o->to]=1;}} void bfs2 (int x) {q.push (x); G[x]=sm[x];int tx;clr (vis,0); while (!q.empty ()) {Tx=q.front (); Q.pop (); vis[tx]=0; TAT (TX) if (G[o->to]<g[tx]+sm[o->to]) {g[o->to]=g[tx]+sm[o->to];if (!vis[o->to]) Q.push (o->to) , Vis[o->to]=1;}}} int main () {int n=read (), M=read (), U,v;rep (i,1,m) U=read (), V=read (), add (u,v), Rep (i,1,n) if (!pre[i]) DFS (i);//rep (i,1, N) printf ("%d", Sccno[i]);p rintf ("\ n");p T=edges;rep (i,1,n) qwq (i) if (Sccno[i]!=sccno[o->to]) aDDE (sccno[i],sccno[o->to]); BFS1 (sccno[1]); BFS2 (sccno[1));//rep (i,1,scc_cnt) printf ("%d%d\n", f[i],g[i]); int Ans=sm[sccno[1]];rep (i,1,scc_cnt) Qaq (i) {if (G[i]&&f[o->to]) Ans=max (Ans,g[i]+f[o->to]-sm[sccno[1]] );} printf ("%d\n", ans); return 0;}

  

3887: [Usaco2015 jan]grass cownoisseur time limit:10 Sec Memory limit:128 MB
submit:179 solved:92
[Submit] [Status] [Discuss] Description

In a effort to better manage the grazing patterns of his cows, Farmer John have installed one-way cow paths all over his F Arm. The farm consists of N fields, conveniently numbered 1..N, and one-way cow path connecting a pair of fields. For example, if-a path connects from field-X to field Y, then cows is allowed to-travel from X to Y and not from Y to X. Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She 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 hers route, since she gets to eat the grass in each one (if she V Isits a field multiple times, she only eats the grass there once). As one might imagine, Bessie is isn't particularly happy about the one-way restriction on FJ ' s paths, since this would likely Reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much GRass She'll be able to eat if she breaks the rules and follows up to one path in the wrong direction. 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 the most once in her journey. In particular, she cannot even take the same path backwards twice.
Give a directed graph and choose a path with a path starting point of 1, and one chance to go in the opposite direction of a certain edge, and ask how many points can be passed? (a point in the path, regardless of how many positive integer number of contributions to the answer are 1)

Inputthe first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M & lt;= 100,000). The following M lines each describe a one-way cow path. Each line contains the distinct field numbers x and y, corresponding to a cow path from X to Y. The same cow path would never appear more than once.

Outputa indicating the maximum number of distinct fields Bessiecan visit along a route starting and ending at Field 1, given that she canfollow at the most one path along this route in the wrong direction.

Sample Input7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7Sample Output6HINT Source

gold& Acknowledgements 18357

bzoj3887: [Usaco2015 jan]grass Cownoisseur

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.