P3119 [USACO15JAN] Grass Cownoisseur G

      ~~~~~      P3119 [USACO15JAN] Grass Cownoisseur G       ~~~~~      总题单链接

思路

      ~~~~~      像这种每个点可以重复经过,但只会统计一次答案的题,可以考虑缩点。

      ~~~~~      缩点之后跑一次以 1 1 1 所在的连通块为起点的最长路,再跑一次以 1 1 1 所在的连通块为终点的最长路(通过建返图实现),然后枚举反边即可。

      ~~~~~      注意跑最长路不能用 D i j k s t r a Dijkstra Dijkstra,我用的 S P F A SPFA SPFA

      ~~~~~      还有就是考虑不走反边的情况,也就是整个图缩成一个连通块的情况。

代码

#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
using namespace std;

vector<ll>eg[100005];
vector<pair<ll,ll>>ng[100005];

ll n,m,S,dis[2][100005];
ll dfn[100005],low[100005],tot;
ll scc[100005],siz[100005],cnt;
ll stk[100005],init[100005],top;

void Tarjan(ll p){
	dfn[p]=low[p]=++tot;
	stk[++top]=p;init[p]=1;
	for(ll v:eg[p]){
		if(!dfn[v]){
			Tarjan(v);
			low[p]=min(low[p],low[v]);
		}
		else if(init[v])low[p]=min(low[p],dfn[v]);
	}
	if(dfn[p]==low[p]){
		cnt++;
		while(1){
			ll v=stk[top--];
			init[v]=0;
			scc[v]=cnt;
			siz[cnt]++;
			if(v==p)break;
		}
	}
}

ll vis[100005];queue<ll>que;
void SPFA(ll opt){
	vis[S]=1;que.push(S);
	while(!que.empty()){
		ll u=que.front();vis[u]=0;que.pop();
		for(pair<ll,ll>it:ng[u]){
			ll v=it.fir,w=it.sec;
			if(dis[opt][v]<dis[opt][u]+w){
				dis[opt][v]=dis[opt][u]+w;
				if(!vis[v])que.push(v),vis[v]=1;
			}
		}
	}
}

signed main(){
	ios::sync_with_stdio(false);
	
	cin>>n>>m;
	while(m--){
		ll x,y;cin>>x>>y;
		eg[x].push_back(y);
	}
	
	for(ll i=1;i<=n;i++)
		if(!dfn[i])Tarjan(i);
	
	if(cnt==1){
		cout<<n;
		return 0;
	}
	
	S=scc[1];
	
	for(ll u=1;u<=n;u++){
		for(ll v:eg[u]){
			if(scc[u]==scc[v])continue;
			ng[scc[u]].push_back({scc[v],siz[scc[v]]});
		}
	}
	
	dis[0][S]=siz[S];SPFA(0);

	for(ll i=1;i<=cnt;i++)ng[i].clear();
	for(ll u=1;u<=n;u++) {
		for(ll v:eg[u]){
			if(scc[u]==scc[v])continue;
			ng[scc[v]].push_back({scc[u],siz[scc[u]]});
		}
	}
	
	dis[1][S]=siz[S];SPFA(1);
	
	ll ans=siz[S];
	for(ll u=1;u<=cnt;u++){
		for(pair<ll,ll>it:ng[u]){
			ll v=it.fir;
			if(dis[0][u]==0||dis[1][v]==0)continue;
			ans=max(ans,dis[0][u]+dis[1][v]);
		}
	}
	cout<<ans-siz[S];
	
	return 0;
}
### USACO P2035 iCow 题目解析 #### 问题描述 Farmer John 购买了一台新的 MP3 播放器 iCow,其中存储了 N (1 ≤ N ≤ 1,000) 首歌曲,每首歌都有一个初始权值 Ri (1 ≤ Ri ≤ 10,000)[^4]。播放顺序由 FJ 设计的独特算法决定: - 下一首播放的是当前所有未播放过的歌曲中权值最高的那一首;若有多个最高权值,则选择编号最小的一首。 - 当某首歌曲播放完成后,其权值会均匀分配给其余 N − 1 首歌曲,并将其自身的权值设为零。 - 如果该歌曲的权值不能被 N − 1 整除,则剩余部分将以 1 单位的形式依次给予排名靠前但尚未获得额外权重的歌曲。 任务是求出按照上述规则最先播放的 T (1 ≤ T ≤ 1000) 首歌曲的具体情况。 #### 解决方案思路 为了模拟这个过程并找到最开始播放的 T 首歌曲,可以采用优先队列(最大堆)来管理待播列表及其对应的权重。每次取出具有最大权重的元素作为即将播放的对象,在更新其他成员的新权重之后重新加入到队列当中继续循环直至达到所需次数为止。 具体步骤如下: - 初始化数据结构:创建一个包含所有歌曲 ID 和它们各自起始分数的最大堆; - 输出此目标的信息; - 更新剩余项目的得分并将已处理项移回至集合内等待下次轮转; 下面给出完整的 C++ 实现代码示例: ```cpp #include <iostream> #include <queue> using namespace std; struct Song { int id; long score; }; bool operator<(const Song& a, const Song& b){ return !(a.score > b.score || (a.score == b.score && a.id < b.id)); } int main(){ priority_queue<Song> pq; int n,t,r; cin>>n>>t; for(int i=1;i<=n;++i){ cin >> r; pq.push({i,r}); } while(t--){ auto top_song=pq.top(); cout<<top_song.id<<"\n"; vector<int> remainders; pq.pop(); if(top_song.score%(n-1)!=0){ for(int j=0;j<top_song.score%(n-1);++j) remainders.push_back(j); } while(!pq.empty()){ Song current = pq.top(); pq.pop(); current.score += top_song.score/(n-1); if (!remainders.empty()) { current.score++; remainders.erase(remainders.begin()); } pq.push(current); } // Reinsert the played song with zero points back into queue. pq.push({top_song.id, 0}); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值