BZOJ 4419: [Shoi2013]发微博

Description

刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录:
! x 表示用户x发了一条微博;
+ x y 表示用户x和用户y成为了好友
- x y 表示用户x和用户y解除了好友关系
当一个用户发微博的时候,所有他的好友(直接关系)都会看到他的消息。
假设最开始所有人之间都不是好友关系,记录也都是合法的(即+ x y时x和y一定不是好友,而- x y时x和y一定是好友)。
问这m条记录发生之后,每个用户分别看到了多少条消息。

Input

第1行2个整数n,m。
接下来m行,按时间顺序读入m条记录,每条记录的格式如题目所述,用空格隔开。

Output

输出一行n个用空格隔开的数(行末无空格),第i个数表示用户i最后看到了几条消息。

Sample Input

2 8
! 1
! 2
+ 1 2
! 1
! 2
- 1 2
! 1
! 2

Sample Output

1 1
只有第4和第5条记录对应的消息被看到过。其他消息发送时,1和2不是好友。
对100%的数据,N<=200000,M<=500000

分析

直接set去做就好了,用一个num[x]表示这个人发了多少条,ans[x]表示他收到了多少条

这个直接暴力去模拟过程就好了、

代码

#include <bits/stdc++.h>

#define N 200010

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}

int n,m;
int ans[N],cnt[N];

std::set<int> S[N];

int main()
{
    int n = read(), m = read();
    char op[2];
    for (int i = 1; i <= m; i++)
    {
        scanf("%c", op);
        if (op[0] == '!')
            cnt[read()]++;
        else
            if (op[0] == '+')
            {
                int x = read(), y = read();
                ans[x] -= cnt[y], ans[y] -= cnt[x];
                S[x].insert(y), S[y].insert(x);
            }
            else
            {
                int x = read(), y = read();
                ans[x] += cnt[y], ans[y] += cnt[x];
                S[x].erase(y), S[y].erase(x);
            }
    }
    std::set<int>::iterator it;
    for (int i = 1; i <= n; i++)
        for (it = S[i].begin(); it != S[i].end(); it++)
            ans[i] += cnt[*it];
    printf("%d",ans[1]);
    for (int i = 2; i <= n; i++)
        printf(" %d",ans[i]);
    printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值