P2860 [USACO06JAN] Redundant Paths G

P2860题:边双连通分量缩点解题思路

      ~~~~~      P2860 [USACO06JAN] Redundant Paths G       ~~~~~      总题单链接

思路

      ~~~~~      我们发现在同一个边双连通分量中的点两两之间多有至少两条路,所以先缩点。

      ~~~~~      缩点后我们将度为 1 1 1 的点两两配对即可。

代码

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

ll head[5005],egt=1;
struct Edge{
	ll v,nxt;
}eg[10005];
void add(ll u,ll v){
	eg[++egt]={v,head[u]};
	head[u]=egt;
}

ll stk[5005],top,cnt;
ll n,m,scc[5005],du[5005];
ll dfn[5005],low[5005],tot;


void Tarjan(ll p,ll ins){
	stk[++top]=p;
	dfn[p]=low[p]=++tot;
	for(ll i=head[p];i;i=eg[i].nxt){
		ll v=eg[i].v;
		if(!dfn[v]){
			Tarjan(v,i);
			low[p]=min(low[p],low[v]);
			if(low[v]>dfn[p]){
				cnt++;
				while(1){
					ll z=stk[top--];
					scc[z]=cnt;
					if(z==v)break;
				}
			}
		}
		else if(ins!=(i^1))low[p]=min(low[p],dfn[v]);
	}
}

signed main(){
	ios::sync_with_stdio(false);
	
	cin>>n>>m;
	while(m--){
		ll x,y;cin>>x>>y;
		add(x,y);add(y,x);
	}
	
	for(ll i=1;i<=n;i++){
		if(!dfn[i]){
			Tarjan(i,0);
			if(top){
				cnt++;
				while(top)scc[stk[top--]]=cnt;
			}
		}
	}
	
	if(cnt==1){
		cout<<0;
		return 0;
	}
	
	for(ll u=1;u<=n;u++)
		for(ll i=head[u];i;i=eg[i].nxt){
			ll v=eg[i].v;
			if(scc[u]!=scc[v])
				du[scc[u]]++,du[scc[v]]++;
		}
	
	ll ans=0;
	for(ll i=1;i<=cnt;i++)
		if(du[i]==2)ans++;
	cout<<ans/2+ans%2;
	
	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、付费专栏及课程。

余额充值