638034 真实社交

· · 个人记录

638034 真实社交

赛时代码

省流:实际上会map,但不知道有c++14,不开map,我是**。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=4e4+5;
bool a[N][N];
int main(){
    ll n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        ll op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            a[x][y]=1;
        }
        if(op==2){
            a[x][y]=0;
        }
        if(op==3){
            if(a[x][y]==1&&a[y][x]==1){
                cout<<"Yes\n";
            }else{
                cout<<"No\n";
            }
        }
    }
    return 0;
}

答案思路

实际上这题就是 map 滴模板题我是**。
但既然是模板题,那我就来写不同的方法~~~。
还记得老师map和set是一起讲的吗,因为他们功能实际上差不多,所以就可以用%%%。
在重温一下set的用法:

ans

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define bo bool
#define str string
template <typename T>
inline void read(T &x){
    x = 0;char ch = getchar();bool f = 0;
    while(ch < '0' || ch > '9'){if(ch == '-')f = 1;ch = getchar();}
    while(ch >= '0' && ch <= '9')x = (x<<1) + (x<<3) + (ch^48),ch=getchar();
    if(f)x = -x;                                       
}
template <typename T,typename ...Args>
inline void read(T &tmp,Args &...tmps){read(tmp);read(tmps...);}
inline void wr(ll x){
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) wr(x / 10);
    putchar(x % 10 + '0');
}
int main(){
    ll n,q;
    cin>>n>>q;
    set<pair<ll,ll>>a;
    while(q--){
        ll op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            //关注
            a.insert({x, y});
        }else if (op==2) {
            //取关
            a.erase({x, y});
        }else {
            //检查 (a,b) 和 (b,a) 是否都存在
            if (a.count({x, y})&&a.count({y, x})) {
                cout << "Yes\n";
            } else {
                cout << "No\n";
            }
        }
    }
    return 0;
}