日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

發(fā)布時間:2023/12/18 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)過盡可能多的草場,貝西在通一個草場只吃一次草,所以一個草場可以經(jīng)過多次。因?yàn)椴輬鍪菃涡械肋B接,這給貝西的品鑒工作帶來了很大的不便,貝西想偷偷逆向行走一次,但最多只能有一次逆行。問,貝西最多能吃到多少個草場的牧草。

輸入格式

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)的單向邊,不會有重復(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.

輸出:

一個數(shù),逆行一次最多可以走幾個草場。

輸入輸出樣例

輸入 #1復(fù)制

輸出 #1復(fù)制

說明/提示

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.

思路:

首先用tarjian縮點(diǎn),縮點(diǎn)后是一個有向無環(huán)圖。每一個點(diǎn)的點(diǎn)權(quán)是他所在的SCC中節(jié)點(diǎn)的個數(shù)。

然后從一號節(jié)點(diǎn)所在的scc塊進(jìn)行SPFA找最長路,可以得到dis1[i] 代表從1所在聯(lián)通塊為始點(diǎn)走到scc_i的最大權(quán)值。

然后反向建邊跑最長路,可以得到dis1[i] 代表從i所在聯(lián)通塊scc_i為始點(diǎn),scc_1 為終點(diǎn)的最大權(quán)值。

然后枚舉所有scc_i,如果scc_1可以到達(dá)scc_i,且 有scc_j為起點(diǎn)到scc_i 為終點(diǎn)的邊,scc_j可以到達(dá)scc_1,則嘗試逆行該邊,更新答案,維護(hù)最大值。

代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}inline void getInt(int* p); const int maxn = 500010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int From[maxn], Laxt[maxn], To[maxn << 2], Next[maxn << 2], cnt; int low[maxn], dfn[maxn], times, qq[maxn], head, scc_cnt, scc[maxn]; bool inst[maxn]; vector<int>G[maxn]; void add(int u, int v) {Next[++cnt] = Laxt[u]; From[cnt] = u;Laxt[u] = cnt; To[cnt] = v; } int c[maxn]; void tarjan(int u) {dfn[u] = low[u] = ++times;qq[++head] = u;inst[u] = 1;for (int i = Laxt[u]; i; i = Next[i]) {if (!dfn[To[i]]) {tarjan(To[i]);low[u] = min(low[u], low[To[i]]);} else if (inst[To[i]]) {low[u] = min(low[u], dfn[To[i]]);}}if (low[u] == dfn[u]) {scc_cnt++;while (true) {int x = qq[head--];scc[x] = scc_cnt;c[scc_cnt]++;inst[x] = 0;if (x == u) { break; }}} }int n, m; std::vector<int> v1[maxn], v2[maxn]; int dis1[maxn]; int dis2[maxn]; queue<int> q; bool vis[maxn]; void spfa1(int S) {dis1[S] = c[S];q.push(S);while (!q.empty()){int now = q.front();q.pop();for (auto y : v1[now]){if (dis1[y] < dis1[now] + c[y]){dis1[y] = dis1[now] + c[y];if (!vis[y]){q.push(y);vis[y] = 1;}}}vis[now] = 0;} } void spfa2(int S) {dis2[S] = c[S];q.push(S);while (!q.empty()){int now = q.front();q.pop();for (auto y : v2[now]){if (dis2[y] < dis2[now] + c[y]){dis2[y] = dis2[now] + c[y];if (!vis[y]){q.push(y);vis[y] = 1;}}}vis[now] = 0;} } int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);du2(n, m);while (m--){int x, y;du2(x, y);add(x, y);}repd(i, 1, n){if (!dfn[i]){tarjan(i);}}repd(u, 1, n){for (int i = Laxt[u]; i; i = Next[i]){if (scc[u] != scc[To[i]]){ // cout<<u<<" "<<To[i]<<" "<<scc[u]<<" "<<scc[To[i]]<<endl;v1[scc[u]].push_back(scc[To[i]]);v2[scc[To[i]]].push_back(scc[u]);}}}spfa1(scc[1]);spfa2(scc[1]);int ans = c[scc[1]];repd(i, 1, scc_cnt){if (vis[i] == 0 && dis1[i]){vis[i] = 1;for (auto y : v2[i]){if (!dis2[y])continue;ans = max(ans, dis1[i] + dis2[y] - c[scc[1]]);}}}printf("%d\n", ans);return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11618854.html

總結(jié)

以上是生活随笔為你收集整理的洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。