QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#614438#1869. Power Station of Artydzr00000AC ✓349ms50156kbC++172.7kb2024-10-05 16:21:372024-10-05 16:21:37

Judging History

你现在查看的是最新测评结果

  • [2024-10-05 16:21:37]
  • 评测
  • 测评结果:AC
  • 用时:349ms
  • 内存:50156kb
  • [2024-10-05 16:21:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int col[1000001],cnt[2][2][2];
int a[1000001],b[1000001];
bool f[1000001],g[1000001];
inline void solve()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        e[i].clear();
        col[i]=-1;
    }
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    string s,t;
    cin>>s;
    int p=0;
    for(auto ch: s)
        f[++p]=(ch=='R');
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    p=0;
    cin>>t;
    for(auto ch: t)
        g[++p]=(ch=='R');
    bool iso=true;
    for(int u=1;u<=n;u++)
        if(col[u]==-1)
        {
            col[u]=0;
            bool bg=true;
            vector<pair<int,int>>nX,nY;
            vector<int>comp;
            auto dfs=[&](auto self,int now)->void
            {
                nX.push_back({a[now],now});
                nY.push_back({b[now],now});
                comp.push_back(now);
                for(auto to: e[now])
                {
                    if(col[to]==-1)
                    {
                        col[to]=(col[now]^1);
                        self(self,to);
                    }
                    else
                        bg&=(col[to]==(col[now]^1));
                }
            };
            dfs(dfs,u);
            sort(nX.begin(),nX.end());
            sort(nY.begin(),nY.end());
            bool ci=true;
            int sz=comp.size();
            for(int i=0;i<sz;i++)
                ci&=(nX[i].first==nY[i].first);
            if(!bg)
            {
                int rA=0,rB=0;
                for(auto v: comp)
                    rA+=f[v],rB+=g[v];
                ci&=((rA&1)==(rB&1));
            }
            else
            {
                for(int i=0;i<sz;i++)
                {
                    int v=nX[i].second,w=nY[i].second;
                    cnt[0][col[v]][f[v]]++;
                    cnt[1][col[w]][g[w]]++;
                    if(i==sz-1||nX[i].first!=nX[i+1].first)
                    {
                        ci&=(cnt[0][0][0]+cnt[0][1][1]==cnt[1][0][0]+cnt[1][1][1]);
                        ci&=(cnt[0][1][0]+cnt[0][0][1]==cnt[1][1][0]+cnt[1][0][1]);
                        memset(cnt,0,sizeof(cnt));
                    }
                }
            }
            iso&=ci;
        }
    puts(iso?"YES":"NO");
}
int main(){
   // freopen("ex_graph1.in","r",stdin);

    int st=clock();

    int t;
    scanf("%d",&t);
    while(t--)
        solve();

    int ed=clock();
    cerr<<"Time: "<<(ed-st)<<"ms"<<endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 33456kb

input:

3
2 1
1 2
3 4
RR
4 3
BB
3 2
1 2
2 3
1 1 1
RBR
1 1 1
BBB
3 3
1 2
2 3
3 1
1 1 1
RBR
1 1 1
BBB

output:

YES
NO
YES

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 349ms
memory: 50156kb

input:

25054
5 10
4 5
4 2
4 3
4 1
5 2
5 3
5 1
2 3
2 1
3 1
12 2 9 10 7
RRRBB
10 2 9 7 12
RRBRB
1 0
4
R
4
R
8 4
4 3
1 5
8 5
6 5
7 2 11 10 9 6 3 5
RBRBBRBR
7 2 10 11 6 5 3 9
RBRBBRBR
7 4
7 2
5 1
5 6
5 4
12 5 9 14 5 12 12
RRBRBRR
14 12 9 5 12 12 5
RBBRBRB
10 1
4 6
5 3 2 9 7 3 11 11 6 8
RRRBRRBBBR
5 3 2 3 7 9 1...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
NO
NO
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
NO
NO...

result:

ok 25054 lines