Codeforces 864F Cities Excursions(离线处理+Tarjan)

F. Cities Excursions
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.

A path from city s to city t is a sequence of cities p1, p2, … , pk, where p1 = s, pk = t, and there is a road from city pi to city pi + 1 for each i from 1 to k - 1. The path can pass multiple times through each city except t. It can’t pass through t more than once.

A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path q from s to t pi < qi, where i is the minimum integer such that pi ≠ qi.

There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city sj and ends in city tj.

For each pair sj, tj help the agency to study the ideal path from sj to tj. Note that it is possible that there is no ideal path from sj to tj. This is possible due to two reasons:

there is no path from sj to tj;
there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.
The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sj to tj).

For each triple sj, tj, kj (1 ≤ j ≤ q) find if there is an ideal path from sj to tj and print the kj-th city in that path, if there is any.

Input
The first line contains three integers n, m and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 3000, 1 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.

Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), denoting that the i-th road goes from city xi to city yi. All roads are one-directional. There can’t be more than one road in each direction between two cities.

Each of the next q lines contains three integers sj, tj and kj (1 ≤ sj, tj ≤ n, sj ≠ tj, 1 ≤ kj ≤ 3000).

Output
In the j-th line print the city that is the kj-th in the ideal path from sj to tj. If there is no ideal path from sj to tj, or the integer kj is greater than the length of this path, print the string ‘-1’ (without quotes) in the j-th line.

Example
input
7 7 5
1 2
2 3
1 3
3 4
4 5
5 3
4 6
1 4 2
2 6 1
1 7 3
1 3 2
1 3 5
output
2
-1
-1
2
-1

题目大意

  有一张V(V3000)的有向图,有Q(Q4×105)组询问,没次询问从一个点到另一个点字典序最小的路径的第k个点。

解题思路

  由于只用3000个点,我们可以考虑对查询离线处理,把所有起点相同的查询放到一组,以这个起点进行dfs,当出现环的时候,后面的点答案都是-1,用Tarjan判定一下有向环即可。总时间复杂度O(V2)

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXV=3000+3;
const int MAXQ=400000+3;

struct Query
{
    int u, v, k, id;
    bool operator < (const Query &other)const
    {
        return u<other.u;
    }
}query[MAXQ];

int V, E, Q;
vector<int> G[MAXV];
vector<Query> save_q[MAXV];
int ans[MAXQ];
int dfn[MAXV], low[MAXV], tmpdfn, path[MAXV], vis[MAXV];

void dfs(int u, int deep, bool cant)
{
    path[deep]=u;
    dfn[u]=low[u]=tmpdfn++;
    vis[u]=1;
    if(!cant)
        for(int i=0;i<save_q[u].size();++i)
            if(save_q[u][i].k<=deep)
                ans[save_q[u][i].id]=path[save_q[u][i].k];
    for(int i=0;i<G[u].size();++i)
    {
        int v=G[u][i];
        if(!vis[v])
        {
            dfs(v, deep+1, cant|(low[u]<dfn[u]));
            low[u]=min(low[u], low[v]);
            cant|=low[v]<dfn[v];
        }
        else if(vis[v]==1)
            low[u]=min(low[u], dfn[v]);
    }
    vis[u]=2;
}

int main()
{
    scanf("%d%d%d", &V, &E, &Q);
    for(int i=0;i<E;++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
    }
    for(int i=1;i<=V;++i)
        sort(G[i].begin(), G[i].end());
    for(int i=0;i<Q;++i)
    {
        scanf("%d%d%d", &query[i].u, &query[i].v, &query[i].k);
        --query[i].k;
        query[i].id=i;
    }
    sort(query, query+Q);
    mem(ans, -1);
    for(int i=0;i<Q;++i)
    {
        save_q[query[i].v].push_back(query[i]);
        if(i==Q-1 || query[i].u!=query[i+1].u)
        {
            tmpdfn=0;
            mem(vis, 0);
            dfs(query[i].u, 0, 0);
            for(int j=1;j<=V;++j)
                save_q[j].clear();
        }
    }
    for(int i=0;i<Q;++i)
        printf("%d\n", ans[i]);

    return 0;
}
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值