P3119 [USACO15JAN] Grass Cownoisseur G [Tarjan + condensing point DP + + + Sequence Topological longest path] [good questions]

Title Description

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.

John has n blocks grassland, numbers 1 to n, which are connected by a plurality of pasture-way street. Bessie the cow pasture is delicious connoisseur, she wanted to reach as many pasture grass to taste.

Bessie always pasture starting from No. 1, and finally back to No. 1 pasture. She wanted to go through as many pastures, Bessie eat once through the grass in a pasture, so many times a pasture. Because pasture is one-way connection, which gives Tasting work Bessie brought great inconvenience, Bessie secretly want to reverse a walk, but can only have one retrograde. He asked Bessie number up to eat grass pasture.

Input Format

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.

Input:

First line: pasture number n, the number of road m.

The m rows, each row indicates a x x and y y unidirectional edge does not duplicate the path there.

Output Format

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.

Output:

A number, a retrograde can go up to several pastures.

Sample input and output

Input # 1
7 10 
1 2 
3 1 
2 5 
2 4 
3 7 
3 5 
3 6 
6 5 
7 2 
4 7 

Output # 1
6 

Description / Tips

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.

 

Thinking

  

 

    Quite a bumpy purple question, the data is not water, and two hours back and forth probably want to write for an hour, wa an hour.

  First, we know that this question is seeking the longest road;

  Second, note that the reverse can only go once this issue, how do the reverse can only go once.

  Put points into three cases to discuss,

  1 \ can walk directly from the No. 1 lawn;

  2 \ 1 can walk the lawn;

  3 \ and No. 1:00 absolutely no relationship.

  This question obviously will not use third this case, we only need to build a positive and a reverse chart diagram, running up the road on the DAG, obviously starting point can be done by the No. 1 running dp node in the topology sequence on it.

  Each flanging last enumerated, ans come from the father is a reverse side of the entry anti forward side of FIG. + Go forward from the entry side of the anti - communication nodes located block size 1 (that is, where wa wear, because the data is a number 1 directly communicating nodes in the block), and maintains the maximum value for ans.

 

CODE

 

  1 #include <bits/stdc++.h>
  2 #define dbg(x) cout << #x << "=" << x << endl
  3 
  4 using namespace std;
  5 typedef long long LL;
  6 const int maxn = 1e5 + 7;
  7 
  8 int head[maxn], dfn[maxn], low[maxn], st[maxn];
  9 int cnt = 0, tot = 0, tim = 0, top = 1, n, m, cl = 0, ans = 0;
 10 int vis[maxn];
 11 int color[maxn];
 12 int sz[maxn];
 13 int dis[maxn][5];
 14 int head1[maxn << 1][5], cnt1, edge1[maxn << 1][5], nxt1[maxn << 1][5];
 15 int in[maxn << 1][5];
 16 
 17 /*
 18 head[],结构体edge:存边
 19 
 20 dfn[],low[]:tarjan中数组
 21 
 22 st[]:模拟栈
 23 
 24 out[]:出边
 25 
 26 sd[]:强连通分量存储
 27 
 28 dq[]:统计答案
 29 */
 30 
 31 template<class T>inline void read(T &res)
 32 {
 33     char c;T flag=1;
 34     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
 35     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
 36 }
 37 
 38 struct Edge{
 39     int nxt, to;
 40 }edge[maxn * 2];
 41 
 42 inline void BuildGraph(int from, int to)
 43 {
 44     cnt++;
 45     edge[cnt].to = to;
 46     edge[cnt].nxt = head[from];
 47     head[from] = cnt;
 48 }
 49 
 50 void tarjan(int x)
 51 {
 52     tim++;
 53     dfn[x] = low[x] = tim;
 54     st[top] = x;
 55     top++;
 56     vis[x] = 1;
 57     for(int i = head[x] ; i != 0; i = edge[i].nxt)
 58     {
 59         int u = edge[i].to;
 60         if(vis[u] == 0)
 61         {
 62             tarjan(u);
 63             low[x]=min(low[x],low[u]);
 64         }
 65         else if(vis[u] == 1)
 66                 low[x]=min(low[x],dfn[u]);
 67     }
 68     if(dfn[x] == low[x])
 69     {
 70         cl++;
 71         do
 72         {
 73             top--;
 74             color[st[top]] = cl;
 75             vis[st[top]] = -1;
 76             sz[color[st[top]]]++;
 77         }while( st[top] != x );
 78     }
 79     return ;
 80 }
 81 
 82 void addedge(int u, int v, int cas) {
 83     if(cas == 1) {
 84         cnt++;
 85     }
 86     in[v][cas]++;
 87     edge1[cnt][cas] = v;
 88     nxt1[cnt][cas] = head1[u][cas];
 89     head1[u][cas] = cnt;
 90 }
 91 
 92 void topo(int cas) {
 93     dis[color[1]][cas] = sz[color[1]];
 94     queue<int> q;
 95     for ( int i = 1; i <= cl; ++i ) {
 96         if(in[i][cas] == 0) {
 97             q.push(i);
 98         }
 99     }
100     while(!q.empty()) {
101         int u = q.front();
102         q.pop();
103         for ( int i = head1[u][cas]; i; i = nxt1[i][cas] ) {
104             int v = edge1[i][cas];
105             dis[v][cas] = max(dis[v][cas], dis[u][cas] + sz[v]);
106             if(--in[v][cas] == 0) {
107                 q.push(v);
108             }
109         }
110     }
111 }
112 
113 int main()
114 {
115     scanf("%d %d",&n, &m);
116     for ( int i = 1; i <= m; ++i ) {
117        int x, y;
118        scanf("%d %d",&x, &y);
119        BuildGraph(x, y);
120     }
121     for ( int i = 1; i <= n; ++i ) {
122         if( !vis[i] ) {
123             tarjan(i);
124         }
125     }
126     cnt = 0;
127     for ( int i = 1; i <= n; ++i ) {
128         for ( int j = head[i]; j; j = edge[j].nxt ) {
129             int v = edge[j].to;
130             if(color[i] != color[v]) {
131                 addedge(color[i], color[v], 1);
132                 addedge(color[v], color[i], 2);
133             }
134         }
135     }
136     memset(dis, 0xef, sizeof(dis));
137     ans = sz[color[1]];
138     topo(1), topo(2);
139     for ( int i = 1; i <= n; ++i ) {
140         for ( int j = head[i]; j; j = edge[j].nxt ) {
141             int v = edge[j].to;
142             if(color[i] != color[v]) {
143                 ans = max(ans, dis[color[v]][1] + dis[color[i]][2] - sz[color[1]]);
144             }
145         }
146     }
147     cout << ans << endl;
148     return 0;
149 }
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/orangeko/p/12381369.html