codeforces1494E. A-Z Graph (MAP二维压缩)

题目:https://codeforces.com/contest/1494/problem/E

题解:二分抄代码

翻译:奇数时有i->j,j->i即可;偶数时有i->j,j->i且这两条路径的字母相同即可。(偶数时,知道这样肯定是正确的,但没有多想--tcl。1->2->3->4,4->3->2->1相等,那么肯定有2->3==3->2,所以这种情况包括了所有情况!!!)

总结:

map<pll,ll> mp;可以优化为map<ll,ll> mp;

其中{u,v}用(u-1)*n+v代替,天秀!!!

代码:

#include <bits/stdc++.h>

#define ll long long
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define rep(i, a, n) for (ll i = a; i <= n; i++)
#define per(i, n, a) for (ll i = n; i >= a; i--)
#define dbg(x) cout << #x << "===" << x << endl
#define dbgg(l, r, x) for(ll i=l;i<=r;i++)cout<<x[i]<<" ";cout<<"<<<"<<#x;cout<<endl
//多动脑,少动笔
using namespace std;

template<class T>void read(T &x){T res=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){res=(res<<3)+(res<<1)+c-'0';c=getchar();}x=res*f;}
void Printf(ll x){if(x<0){putchar('-');x=-x;}if(x>9)Printf(x/10);putchar(x%10+'0');}
void print(ll x,char c){Printf(x),putchar(c);}
const ll maxn = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll inf = 1e9;


unordered_map<ll,ll> mp;
ll n,m;
ll u,v,k;
char ch,c;
ll ed(ll u,ll v){
    return (u-1)*n+v;//模拟二维,懂的都懂,可以省时间
}
ll cnt1,cnt2;
int main()
{
    //freopen("testdata.in","r",stdin);
    //freopen("testout.out","w",stdout);
    ll TT=1;
    // read(TT);
    while(TT--){
        read(n),read(m);
        while(m--){
            // dbg(m);
            scanf(" %c",&ch);
            // dbg(ch);
            if(ch=='+'){
                read(u),read(v);
                scanf(" %c",&c);
                mp[ed(u,v)]=c-'a'+1;
                if(mp[ed(v,u)]){
                    cnt1++;
                    if(mp[ed(v,u)]==c-'a'+1) cnt2++;
                }
            }           
            else if(ch=='-'){
                read(u),read(v);
                if(mp[ed(v,u)]){
                    cnt1--;
                    if(mp[ed(v,u)]==mp[ed(u,v)]) cnt2--;
                }
                mp[ed(u,v)]=0;
            }
            
            else{
                read(k);
                bool f=false;
                if(k%2&&cnt1) f=true;
                if(k%2==0&&cnt2) f=true;
                puts(f?"YES":"NO");
            }
        }
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值