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

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 200003
using 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]);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值