首页 > 其他分享 >POI2006MET-Subway

POI2006MET-Subway

时间:2024-04-15 19:44:05浏览次数:25  
标签:POI2006MET int dep Subway push return define deg

POI #Year2006 #妙妙题 #贪心

考虑从下往上按照拓扑序分层,对于每一层,这一层最多可以选择 \(min(2m,cnt)\) 个

考虑这个上界是否可以达到,这是一定可以的,通过将在下面结束的路径向上,可以做到每个点都被经过

所以直接统计

// Author: xiaruize
#ifndef ONLINE_JUDGE
#define debug(x) cerr << "On Line:" << __LINE__ << #x << "=" << x << endl
bool start_of_memory_use;
#else
#define debug(x)
#endif
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
clock_t start_clock = clock();
#endif
// #define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define per(i, n, a) for (int i = (n); i >= (a); --i)
int max(int a, int b)

{
	if (a > b)
		return a;
	return b;
}
int min(int a, int b)
{
	if (a < b)
		return a;
	return b;
}
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 1e6 + 10;

int n, m;
vector<int> g[N];
int cnt[N], dep[N];
int deg[N];

void solve()
{
	cin >> n >> m;
	rep(i, 1, n - 1)
	{
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
		deg[u]++;
		deg[v]++;
	}
	queue<int> q;
	rep(i, 1, n)
	{
		if (deg[i] == 1)
		{
			dep[i] = 1;
			q.push(i);
		}
	}
	while (!q.empty())
	{
		int x = q.front();
		q.pop();
		cnt[dep[x]]++;
		for (auto v : g[x])
		{
			dep[v] = dep[x] + 1;
			deg[v]--;
			if (deg[v] == 1)
				q.push(v);
		}
	}
	int res = 0;
	rep(i, 1, n)
	{
		res += min(cnt[i], m * 2);
		// cerr << cnt[i] << ' ';
	}
	cout << res << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
		solve();
#ifndef ONLINE_JUDGE
	cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
	cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
	return 0;
}

标签:POI2006MET,int,dep,Subway,push,return,define,deg
From: https://www.cnblogs.com/xiaruize/p/18136795

相关文章

  • G. Rudolf and Subway
     原题的无向图等价于上图所示的联通图,此时我们要求的就是起始位置到终止位置最少要经过几个有颜色的结点。code #include<bits/stdc++.h>usingnamespacestd;constintN=4e5+5;intvis[N];intmain(){//freopen("input.txt","r",stdin);intt;cin>>t;......
  • [Rudolf and Subway]
    RudolfandSubway题目大意给定一个\(n\)个点,\(m\)条边的无向图,第\(i\)条边表示\(x,y\)是\(z\)号线的相邻站点,问\(s\)到\(t\)最少需要换乘多少次做法用分层图,对于任意一条线路,让他们单独分为一层,如果一个点既在\(1\)号线又在\(2\)号线,那么它应该处于两个图层中,举个例子441......
  • CF371E Subway Innovation 题解
    题目链接:CF或者洛谷对于绝对值的几何意义来说,这题是在直线上的两点间的距离,为了总的距离和最下,首先最好让它们两两之间最好都紧挨着。由于询问的是\((i,j)\)不重不漏的对有关,即\((i<j)\+\(i>j)\+\(i=j)=all(i,j)\),又因为,\((i,j)\)的贡献和\((j,i)\)相同且重复,所以我......
  • 题解:CF1941G Rudolf and Subway
    原题链接简化题意一个无向连通图中将边分成了不同颜色(保证同种颜色联通),问从\(b\)到\(e\)最短需要经过几种颜色思路考虑因为同种颜色联通,可直接在读入的时候开两个vector分别存每个点属于的颜色及每种颜色有哪些点,又因为颜色数字可能跨度比较大,最好另开一个存颜色的种......
  • Codeforces Round 933 G. Rudolf and Subway
    原题链接:Problem-G-Codeforces思路:根据题意可知相同颜色的边一定是联通的,那么就可以设置虚点,例如1-2,2-3,3-4边的颜色都是相同的,那么就可以设置一个特殊的点例如设置为10,那么这三条边就可以改成1-10,10-2,2-10,10-3,3-10,10-4,从点到虚点需要1的代价,但是从虚点到其他点不需要代价,......
  • G. Rudolf and Subway
    原题链接题解太巧妙了!!原题等效于该分层图,然后广搜本题中我用了另一种方法建边,因为清空太麻烦了code#include<bits/stdc++.h>usingnamespacestd;intmain(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);intt;cin>>t;while(t--)......
  • POJ1635subway tree system
    在扫描过程中一旦扫描到一个子串01数量相等了,这个时候肯定是已经递归回到根节点了,因为从根节点下去的一步操作给了一个0,而这个0一定要从这条边回到根节点才能产生一个1与其匹配(这个1不可能来自其他边的回溯,因为其他边的回溯的前提就是之前从这条边下去了,就会产生一个0,,这个0就要......
  • CF131D Subway 题解
    题目传送门前置知识强连通分量|最短路解法考虑用Tarjan进行缩点,然后跑最短路。缩点:本题的缩点有些特殊,基于有向图缩点修改而得,因为是无向图,所以在Tarjan过程中要额外记录一下从何处转移过来,防止在同一处一直循环。基环树上找环还有其他方法,这里仅讲解使用Tarjan求......
  • 1131 Subway Map
    题目:Inthebigcities,thesubwaysystemsalwayslooksocomplextothevisitors.Togiveyousomesense,thefollowingfigureshowsthemapofBeijingsubway.Nowyouaresupposedtohelppeoplewithyourcomputerskills!Giventhestartingpositionofy......
  • CF1060E Sergey and Subway
    题目大意给定一棵树,每两个有边直接相连的点之间距离为\(1\)。现在我们要给所有原来距离为\(2\)的城市之间修一条长度为\(1\)的道路。记\(\operatorname{dis}(a,b)\)表示\(a,b\)之间的最短距离,求\[\sum_{i=1}^n\sum^{n}_{j=i+1}\operatorname{dis}(i,j)\]思路考虑修......