首页 > 其他分享 >POI2010TEL-Teleportation

POI2010TEL-Teleportation

时间:2024-04-18 09:11:39浏览次数:17  
标签:int res Teleportation vis POI2010TEL cnt3 cnt2 cnt1

分层图 #贪心 #POI #Year2010

考虑将答案的图建成一个 \(5\) 层的图,其中 \(1,2\) 为第 \(1,5\) 层,第 \(2,4\) 层为已经与 \(1,2\) 相连的点

考虑将剩下的点与第 \(2,4\) 层相连,贪心选尽可能大的

// Author: xiaruize
const int N = 2e5 + 10;

int n, m;
vector<int> g[N];
int cnt1, cnt2, cnt3;
int vis[N];
int res = 0;

void solve()
{
	cin >> n >> m;
	rep(i, 1, m)
	{
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for (auto v : g[1])
	{
		cnt1++;
		vis[v] = 1;
	}
	for (auto v : g[2])
	{
		vis[v] = 2;
		cnt2++;
	}
	cnt3 = n - cnt1 - cnt2 - 2;
	res = (cnt3 - 1) * cnt3 / 2 + cnt2 * (cnt2 - 1) / 2 + cnt1 * (cnt1 - 1) / 2;
	rep(x, 1, n)
	{
		if (!vis[x])
		{
			int tot = 0;
			int flag = 0;
			for (auto v : g[x])
			{
				if (vis[v])
				{
					tot++;
					flag = vis[v];
				}
				else if (v > x)
					res--;
			}
			if (flag == 1)
				res += cnt1 - tot;
			else if (flag == 2)
				res += cnt2 - tot;
			else
				res += max(cnt1, cnt2);
		}
		else
		{
			for (auto v : g[x])
				if (vis[v] && v > x)
					res--;
		}
	}
	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;
}

标签:int,res,Teleportation,vis,POI2010TEL,cnt3,cnt2,cnt1
From: https://www.cnblogs.com/xiaruize/p/18142214

相关文章

  • LG-P4264 [USACO18FEB]Teleportation S 题解
    LG-P4264[USACO18FEB]TeleportationSSolution目录LG-P4264[USACO18FEB]TeleportationSSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入......