bzoj 4419: [Shoi2013]发微博 (STL)

来源:互联网 发布:跑车手机主题软件 编辑:程序博客网 时间:2024/05/14 22:18

4419: [Shoi2013]发微博

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 431  Solved: 239
[Submit][Status][Discuss]

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

HINT

Source

[Submit][Status][Discuss]

题解:STL

用STL强行水过,貌似set,map都可以

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#define N 200003using namespace std;map<int,int> mp[N];int n,m,cnt[N],ans[N];int main(){freopen("a.in","r",stdin);scanf("%d%d",&n,&m);for (int i=1;i<=m;i++) {char opt[10];int x,y;scanf("%s",opt+1);if (opt[1]=='!') {scanf("%d",&x);cnt[x]++;} if (opt[1]=='+'){scanf("%d%d",&x,&y); mp[x][y]=cnt[y]; mp[y][x]=cnt[x];}if (opt[1]=='-') {scanf("%d%d",&x,&y);ans[x]+=cnt[y]-mp[x][y]; mp[x].erase(y);ans[y]+=cnt[x]-mp[y][x]; mp[y].erase(x);}}for (int i=1;i<=n;i++) {map<int,int>::iterator it;for (it=mp[i].begin();it!=mp[i].end();++it) { ans[i]+=cnt[it->first]-(it->second);    } printf("%d%c",ans[i]," \n"[i==n]);}}



0 0
原创粉丝点击