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");
}