zoukankan      html  css  js  c++  java
  • P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    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,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

    贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

    输入输出格式

    输入格式:

    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.

    输出格式:

    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.

    输入输出样例

    输入样例#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.

    分析

    一道好题。

    题意:一张有向图图,求可以改变一条边的方向,之后的最大的强联通分量(包含1)。

    思路:首先求出强连通分量,缩点,这没啥好说的,

    然后就是改变一条边的方向,使得有向无环图从1开始走,再返回1,所经过的点最多。

    首先明确:因为只改一条边,所以可以枚举是那条边。因为从1开始走,再回到1,所以可以求出每个点到1的最大距离(所经过的点最多)。

    那么问题是如何求每个点到1和1到每个点的最大距离,

    可以dfs,不过有这样一种情况:在搜到2,及更新2的后代之后,又发现了3,那么2及2的后代又要全更新一次。

     

    为了改变这种情况,所以可以拓扑排序。

    之后枚举边,假设修改它,那么修改它的答案就是这条边的起点到1的距离,1到这条边的终点的距离。取个最大值

    code

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<cstdlib>
      6 #include<vector>
      7 #include<iostream>
      8 
      9 using namespace std;
     10 
     11 const int N = 200100;
     12 const int M = 200100;
     13 struct Edge{
     14     int to,nxt;
     15 }e[N];
     16 int head[N],dfn[N],low[N],st[N],bel[N],sz[N];
     17 int dis[N],fdis[N],ru[N],fru[N];
     18 int q[1000000],L,R;
     19 bool vis[N],pd[N],fpd[N];
     20 int cnt,tn,tot,top;
     21 vector<int>s[N];
     22 vector<int>s2[N];
     23 
     24 inline char nc() {
     25     static char buf[100000],*p1 = buf,*p2 = buf;
     26     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF : *p1++;
     27 }
     28 inline int read() {
     29     int x = 0,f = 1;char ch = nc();
     30     for (; ch<'0'||ch>'9'; ch = nc()) 
     31         if (ch=='-') f = -1;
     32     for (; ch>='0'&&ch<='9'; ch = nc()) 
     33         x = x*10+ch-'0';
     34     return x * f;
     35 }
     36 
     37 inline void add_edge(int u,int v) {
     38     e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
     39 }
     40 
     41 void tarjan(int u) {
     42     dfn[u] = low[u] = ++tn;
     43     st[++top] = u;
     44     vis[u] = true;
     45     for (int i=head[u]; i; i=e[i].nxt) {
     46         int v = e[i].to;
     47         if (!dfn[v]) {
     48             tarjan(v);
     49             low[u] = min(low[u],low[v]);
     50         }
     51         else if (vis[v]) 
     52             low[u] = min(low[u],dfn[v]);
     53     }
     54     if (low[u]==dfn[u]) {
     55         ++cnt;
     56         do { 
     57             vis[st[top]] = false;
     58             bel[st[top]] = cnt;
     59             sz[cnt]++;
     60             top--;
     61         } while (st[top+1] != u);
     62     }
     63 }
     64 void search1(int u) {
     65     pd[u] = true;
     66     int tmp = s[u].size();
     67     for (int i=0; i<tmp; ++i) {
     68         int v = s[u][i];
     69         ru[v]++;
     70         if (!pd[v]) search1(v);
     71     }
     72 }
     73 void topo1() {
     74     search1(bel[1]);
     75     L = 1,R = 0;
     76     q[++R] = bel[1];
     77     dis[bel[1]] = sz[bel[1]];
     78     while (L <= R) {
     79         int u = q[L++];
     80         int tmp = s[u].size();
     81         for (int i=0; i<tmp; ++i) {
     82             int v = s[u][i];
     83             dis[v] = max(dis[v],dis[u]+sz[v]);
     84             ru[v]--;
     85             if (ru[v]==0) q[++R] = v;
     86         }
     87     }
     88 }
     89 void search2(int u) {
     90     fpd[u] = true;
     91     int tmp = s2[u].size();
     92     for (int i=0; i<tmp; ++i) {
     93         int v = s2[u][i];
     94         fru[v]++;
     95         if (!fpd[v]) search2(v);
     96     }
     97 }
     98 void topo2() {
     99     search2(bel[1]);
    100     L = 1,R = 0;
    101     q[++R] = bel[1];
    102     fdis[bel[1]] = sz[bel[1]];
    103     while (L <= R) {
    104         int u = q[L++];
    105         int tmp = s2[u].size();
    106         for (int i=0; i<tmp; ++i) {
    107             int v = s2[u][i];
    108             fdis[v] = max(fdis[v],fdis[u]+sz[v]);
    109             fru[v]--;
    110             if (fru[v]==0) q[++R] = v;
    111         }
    112     }
    113 }
    114 int main() {
    115 
    116     int n = read(),m = read();
    117     for (int i=1; i<=m; ++i) {
    118         int u = read(),v = read();
    119         add_edge(u,v);
    120     }
    121     for (int i=1; i<=n; ++i) 
    122         if (!dfn[i]) tarjan(i);
    123     for (int u=1; u<=n; ++u) {
    124         for (int i=head[u]; i; i=e[i].nxt) {
    125             int v = e[i].to;
    126             if (bel[u]!=bel[v]) {
    127                 s[bel[u]].push_back(bel[v]);
    128                 s2[bel[v]].push_back(bel[u]);
    129             }
    130         }
    131     }
    132 
    133     topo1();
    134     topo2();
    135     int ans = 0;
    136     for (int i=1; i<=cnt; ++i) {
    137         int tmp = s[i].size();
    138         for (int j=0; j<tmp; ++j) {
    139             if (i==s[i][j]||!pd[s[i][j]] || !fpd[i]) continue;
    140             ans = max(ans,dis[s[i][j]]+fdis[i]-sz[bel[1]]);
    141         }
    142     }
    143     printf("%d",ans);
    144     return 0;
    145 }
  • 相关阅读:
    家庭养花秘笈1000问
    生活中来3000例·健康篇
    生命的奥秘百科(套装共10册)
    海军陆战队6:太空战舰
    历史文明探秘百科(套装共10册)
    中医养生知识读本
    上工养生话刮痧
    古法艾灸
    钻井液处理剂及其作用原理
    重金属污泥处理技术与管理
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7898276.html
Copyright © 2011-2022 走看看