当前位置 : 主页 > 编程语言 > c语言 >

Maratona de Programa¸c˜ao da SBC 2013 Sub-Regional Brasil do ACM ICPC C.Boss 拓扑排序

来源:互联网 收集:自由互联 发布时间:2023-09-07
Everyone knows Iks, the last trend in social network, which made so much success that competitors like Facebook and Google+ are strugling to keep their own social networks in business. As several “.com” companies, Iks started in a small

Everyone knows Iks, the last trend in social network, which made so much success that competitors
like Facebook and Google+ are strugling to keep their own social networks in business. As several
“.com” companies, Iks started in a small garage, but today employs hundreds of thousands.
Iks has also a non-standard management system. For example, there are no committees or boards,
which means less time spent on internal meetings. However, as usual in other companies, there is
a chain (or rather several chains) of command, as a person may manage other employees, and may
be managed by other employees. The figure below shows the chain of command for some employees,
along with their ages.
Alice, 21
Clara, 26
David, 33 Elaine, 33
Fred, 22
George, 18
Bia, 42
Alice, 21
Clara, 26
George, 18 Elaine, 33
Fred, 22
David, 33
Bia, 42
(a) (b)
A person P1 may manage another person P2 directly (when P1 is the immediate superior of P1)
or indirectly (when P1 manages direclty a person P3 who manages P2 directly or indirectly). For
example, in the figure above, Alice manages David directly and Claire indirectly. A person does not
manage himself/herself, either directly or indirectly.
One folklore that developed in Wall Street is that Iks is so successfull because in its chain of
command a manager is always younger than the managed employee. As we can see in figure above,
that is not true. But this folklore
Maratona de Programa¸c˜ao da SBC – ACM ICPC – 2013 5
described by a line containing the identifier T followed by two integers A and B, indicating the two
employers that must swap places in the chain of command. An instruction of type query is described
by a line containing the identifier P followed by one integer E, representing the number of an employer.
The last instruction is of type query.
Output
For each instruction of type query your program must print a single line containing a single integer,
the age of the employee who is the youngest person that manages the employer named in the query
(directly or indirectly), if that person exists. If the employee does not have a manager, print an *
(asterisc).
Restrictions
• 1 ≤ N ≤ 500
• 0 ≤ M ≤ 60 × 103
• 1 ≤ I ≤ 500
• 1 ≤ Ki ≤ 100, for 1 ≤ i ≤ N
• 1 ≤ X, Y ≤ N, X 6= Y
• 1 ≤ A, B ≤ N
• 1 ≤ E ≤ N
Examples
Input
7 8 9
21 33 33 18 42 22 26
1 2
1 3
2 5
3 5
3 6
4 6
4 7
6 7
P 7
T 4 2
P 7
P 5
T 1 4
P 7
T 4 7
P 2
P 6
Output
18
21
18
18
*
26
Maratona de Programa¸c˜ao da S

一个有向图,每个 employee有直接或间接的 manager,有两种操作:
①:交换A,B 两人;
②:询问一个employee 的Boss中最年轻的年龄;

拓扑排序;
然后每次将操作更新即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>

typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}
int N, M, I;
int head[maxn];
int value[maxn];
int degree[maxn];
int u[maxn], v[maxn];
int num;
bool vis[maxn];
int p[maxn];
int x[maxn];

void init() {
    memset(head, -1, sizeof(head));
}
struct note {
    int from, to, pre;
}edge[maxn];


void addedge(int u, int v) {
    edge[num] = note{ u,v,head[u] };
    head[u] = num++;
}
void pushUp(int n) {
    memset(vis, 0, sizeof(vis));
    queue<int>q;
    int i, j;
    memcpy(degree, v, sizeof(degree));
    for (i = 1; i <= n; i++) {
        if (degree[i] == 0)q.push(i), vis[i] = 1;
    }
    memset(value, 0x3f, sizeof(value));
    while (!q.empty()) {
        int nw = q.front();
        q.pop();
        for (i = head[nw]; i != -1; i = edge[i].pre) {
            int to = edge[i].to;
            value[to] = min(value[to], value[nw]);
            value[to] = min(value[to], u[nw]);
            degree[to]--;
            if (degree[to] == 0) {
                if (!vis[to]) {
                    vis[to] = 1;
                    q.push(to);
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> N >> M >> I;
    num = 0;
    init();
    int i, j;
    for (i = 1; i <= N; i++) {
        cin >> u[i];
        p[i] = x[i] = i;
    }
    for (i = 1; i <= M; i++) {
        int U, V;
        cin >> U >> V;
        addedge(U, V);
        v[V]++;
    }
    pushUp(N);
    char ch[10];
    for (i = 1; i <= I; i++) {
        cin >> ch;
        if (ch[0] == 'P') {
            int U;
            cin >> U;
            if (value[p[U]] == inf)cout << "*" << endl;
            else cout << value[p[U]] << endl;
        }
        else {
            int U, V;
            cin >> U >> V;
            int t = u[p[U]];
            u[p[U]] = u[p[V]];
            u[p[V]] = t;

            t = p[U];
            p[U] = p[V];
            p[V] = t;
            pushUp(N);
        }
    }
    return 0;
}
上一篇:Gym - 101341D
下一篇:没有了
网友评论