AtCode 209 C(思维,去重,easy D (最短路+思维

C - Not Equal
贪心排序不影响答案
去重过程类似阶乘

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 9;
int n;
int a[maxn];
int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	sort(a+1,a+1+n);
	ll ans = 1;
	for(int i = 1; i <= n; ++i)
		ans = ans * max(0, a[i] - i + 1) % mod;
	cout << ans << endl;
	return 0;
}

D - Collision
题意:n个点 n-1条边,每条边长度相等。每次查询,A在c点,B在d点,他们运动速度相同,A往d点走,B往c点走,问他们可能相遇在边上还是节点上。
分析:先跑一边1到其他点的最短路,我们给距离1号节点为偶数点的涂红色,距离为奇数的涂蓝色。一个人开始在红色节点,下一次一定去蓝色节点,反之亦然。所以如果dis[c] ,dis[d] 奇偶性相同,就可以在城镇相遇,否则相遇只会在路上

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 3e5 + 9;
int dis[maxn];
int v[maxn];
int head[maxn], cnt;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
struct node
{
	int w, to, nextt;
}e[maxn];
void add_e(int x,int y)
{
	e[++cnt].to = y;
	e[cnt].w = 1;
	e[cnt].nextt = head[x];
	head[x] = cnt;
}
int main()
{
	int n, m;
	cin >> n >> m;
	memset(dis,0x3f,sizeof(dis));
	dis[1] = 0;
	q.push(make_pair(0,1));
	for(int i = 1; i < n; ++i)
	{
		int a, b;
		cin >> a >> b;
		add_e(a, b);
		add_e(b, a);
	}
	while(!q.empty())
	{
		int now = q.top().second;
		q.pop();
		if(v[now]) continue;
		v[now] = 1;
		for(int i = head[now]; i; i = e[i].nextt)
		{
			int tmp = e[i].to;
			if(!v[tmp] && dis[tmp] > dis[now] + e[i].w)
			{
				dis[tmp] = dis[now] + e[i].w;
				q.push(make_pair(dis[tmp], tmp));
			}
		}
	}
	for(int i = 1; i <= m; ++i)
	{
		int a, b;
		cin >> a >> b;
		if(dis[a]%2 == dis[b] % 2) cout << "Town\n";
		else cout << "Road\n";
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值