程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Twelves Monkeys (multiset解法 141

Twelves Monkeys (multiset解法 141

編輯:關於C++

 

Twelves Monkeys

Time Limit: 5 Seconds Memory Limit: 32768 KB

 

James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus so deadly that it forced the survivors to move underground. In the years that followed, scientists had engineered an imprecise form of time travel. To earn a pardon, Cole allows scientists to send him on dangerous missions to the past to collect information on the virus, thought to have been released by a terrorist organization known as the Army of the Twelve Monkeys.

The time travel is powerful so that sicentists can send Cole from year x[i] back to year y[i]. Eventually, Cole finds that Goines is the founder of the Army of the Twelve Monkeys, and set out in search of him. When they find and confront him, however, Goines denies any involvement with the viruscan. After that, Cole goes back and tells scientists what he knew. He wants to quit the mission to enjoy life. He wants to go back to the any year before current year, but scientists only allow him to use time travel once. In case of failure, Cole will find at least one route for backup. Please help him to calculate how many years he can go with at least two routes.

Input

The input file contains multiple test cases.

The first line contains three integers n,m,q(1≤ n ≤ 50000, 1≤ m ≤ 50000, 1≤ q ≤ 50000), indicating the maximum year, the number of time travel path and the number of queries.

The following m lines contains two integers x,y(1≤ y x ≤ 50000) indicating Cole can travel from year x to year y.

The following q lines contains one integers p(1≤ p ≤ n) indicating the year Cole is at now

Output

For each test case, you should output one line, contain a number which is the total number of the year Cole can go.

Sample Input

9 3 3
9 1
6 1
4 1
6
7
2

Sample Output

5
0
1

Hint

6 can go back to 1 for two route. One is 6-1, the other is 6-7-8-9-1.6 can go back to 2 for two route. One is 6-1-2, the other is 6-7-8-9-1-2.


題意:n個時刻點,m次時光穿梭,告訴每次穿梭的起點和終點,q次詢問,每次詢問t時刻t之前有多少時刻點是可以通過兩種不同的路徑到達的。

思路:對於詢問的時刻t可以順時間向後推移到t+1,t+2,t+3.。。。。。那麼t時刻及以後的時刻的穿梭都是可能的,把他們能穿梭到的時刻插入multiset,如果multiset裡有至少兩個元素的值大於等於t,則該時刻t存在解。另外注意的是,詢問的時刻點靠前的都可以到達靠後的,所以我們得從後往前求解。

代碼:

 

#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#pragma comment (linker,/STACK:102400000,102400000)
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf(%d, &n)
#define sff(a,b)    scanf(%d %d, &a, &b)
#define sfff(a,b,c) scanf(%d %d %d, &a, &b, &c)
#define pf          printf
#define DBG         pf(Hi
)
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 1e5+10;

struct Edge
{
    int u,v;
}edge[MAXN];

struct Node
{
    int d,id;
}node[MAXN];

int cmp1(Edge e1,Edge e2)
{
    return e1.u S;

int n,m,q;
int ans[MAXN],a[maxn];

void solve()
{
    int i;
    S.clear();
    sort(edge,edge+m,cmp1); //兩個都按照日期從小到大排序
    sort(node,node+q,cmp2);
    int pos=m-1;
    for (i=q-1;i>=0;i--)    //從後往前掃,因為前面的時刻可以順時間到達後面的
    {
        int day=node[i].d;
        int cnt=0;
        while (pos>=0&&edge[pos].u>=day)
        {
            S.insert(edge[pos].v);
            pos--;
        }
        for (multiset::iterator it=S.begin();it!=S.end();it++)
        {
            a[cnt++]=*(it);
            if (cnt>=2) break;
        }
        if (cnt>=2&&a[1]<=day)
            ans[node[i].id]=day-a[1];
        else
            ans[node[i].id]=0;
    }
    for (i=0;i

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved