[Usaco15jan] Grass identification grass cownoisseur

Source: Internet
Author: User

"Title description":
[Usaco15jan] Grass identification grass cownoisseur

"Ideas":
First of all, let's consider that Bessie does not take the opposite edge, so for any strongly connected component \ (e\) , it is easy to know:

    • \ (\forall u,v \in e\),\ (\exists u \to v \ and \ v \to u\)

\ (\because\) Bessie only eats grass once every time she passes through a pasture,\therefore\ can be shrunk, and then a \ (dag\)is obtained to count each of the strong connected components \ ( size\) value that indicates how many points are in this strongly connected component, and then run \ (dp\) on \ (dag\) .

But the problem is that Bessie plays snake skin, and she can be retrograde once.

So we should change a way of thinking, after shrinking points, considering the situation of retrograde, we can put the positive and negative of the chart is built once, set 1th points where the strong connected component is \ (e\), find out \ (dis1[]\) and \ (dis2[]\), Represents the longest road with \ (e\) as the starting point and the midpoint with \ (e\) , and then enumerates each \ (i\), then the answer is:

    • \ (Max \{dis1[i] +dis2[v]-size[e]\}\).

This is the equivalent of taking the opposite side.

#include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define INF 0x3f3f3f3fusing namespace Std;int n,m;const int maxn = 100005;struct edge{int u,v,nxt;} E[maxn];int head[maxn];int cnt = 0;int dfn[maxn];int low[maxn];int id = 0;int S[maxn];int tot = 0;int Bcnt = 0;int X[MAXN] ; int Y[maxn];bool vis[maxn];int size[maxn];int b[maxn];bool back[maxn];int dis1[maxn];int dis2[MAXN];int ans =-inf; inline void Add (int u,int v) {e[++cnt].u = U;E[CNT].V = V;E[CNT].NXT = Head[u];head[u] = cnt;}    inline void Tarjan (int u) {low[u] = dfn[u] = ++id;    S[++tot]=u;vis[u] = 1;        for (int i=head[u];i;i=e[i].nxt) {int v = E[I].V;            if (!dfn[v]) {Tarjan (v);        Low[u] = min (Low[u], low[v]);    } else if (Vis[v]) low[u] = min (Low[u], dfn[v]); } if (dfn[u] = = Low[u]) {int j=0;        bcnt++;            while (j ^ u) {j = s[tot--];            VIS[J] = 0;            B[J] = bcnt;        size[bcnt]++;  }  }}inline BOOL CMP (int a,int b) {return a > B;}    Queue<int>q;inline void dp (int *dis) {memset (dis,0,sizeof dis);    memset (vis,0,sizeof Vis);    DIS[B[1]] = size[b[1]];        Q.push (b[1]);        while (!q.empty ()) {int u = q.front (); Q.pop (); vis[u] = 0;            for (int i=head[u];i;i=e[i].nxt) {int v = E[I].V;                if (Dis[v] < Dis[u] + Size[v]) {Dis[v] = Dis[u] + size[v];                    if (!vis[v]) {Q.push (v);                VIS[V] = 1;    }}}}}int main () {scanf ("%d%d", &n,&m);        for (int i=1;i<=m;++i) {int u,v;scanf ("%d%d", &u,&v);        Add (U,V);    X[i] = u;y[i] = v;        } for (int i=1;i<=n;++i) {if (!dfn[i]) {Tarjan (i);    }} memset (head,0,sizeof head); cnt = 0;        for (int i=1;i<=m;++i) {if (B[x[i] ^ b[y[i]]) {Add (B[x[i]], b[y[i]]);   }} for (int i=head[b[1]];i;i=e[i].nxt) {     int v = E[I].V;    BACK[V] = 1;        } DP (DIS1);    memset (head,0,sizeof head); cnt = 0;    memset (back,0,sizeof back);        for (int i=1;i<=m;++i) {if (B[x[i] ^ b[y[i]]) {Add (B[y[i]], b[x[i]]);        }} for (int i=head[b[1]];i;i=e[i].nxt) {int v = E[I].V;    BACK[V] = 1;        } DP (DIS2);        for (int i=1;i<=bcnt;++i) {if (!dis1[i]) continue;            for (int j=head[i];j;j=e[j].nxt) {int v = E[J].V;            if (!dis2[v]) continue;        ans = max (ans, (dis1[i] + dis2[v]-size[b[1]));    }} printf ("%d", ans); return 0;}

[Usaco15jan] grass identification 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.