QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619541#7900. Gifts from KnowledgeS1yuanWangWA 26ms52444kbC++143.8kb2024-10-07 14:36:342024-10-07 14:36:38

Judging History

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

  • [2024-10-07 14:36:38]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:52444kb
  • [2024-10-07 14:36:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int mod = 1e9 + 7;
ll n,m;
vector<char>a[1000006];
int fa[1000005];
// 初始化
void init(int n)
{
    for (int i = 1; i <= n; i++)
        fa[i] = i;
}

// 找父节点,路径压缩
int find(int i)
{
    if (fa[i] == i)
        return i;
    else
    {
        fa[i] = find(fa[i]); // 路径压缩
        return fa[i];
    }
}
// 合并
void bing(int i, int j)
{
    int i_fa = find(i);
    int j_fa = find(j);
    fa[i_fa] = j_fa;
}
ll quickp(ll a,ll b){
    ll ans = 1;
    while(b > 0){
        if(b&1){
            ans = ans * a %mod;
        }
        b >>= 1;
        a = a*a%mod;
    }
    return ans%mod;
}
struct edgeto{
    ll a,b;
};
vector <edgeto> g[1000005];
vector <ll> color;
ll flag = 1;
void dfs(ll x,ll c){
    color[x] = c;
    for(auto to : g[x]){
        if(color[to.a] == -1&&to.b == 2) 
        //如果未涂色 边是2 涂相反颜色
            dfs(to.a,c^1);
        else if(color[to.a] == -1&&to.b == 1){  
        //如果未涂色 边是1 涂相同颜色
            dfs(to.a,c);
        }
        else{
            //已经有颜色
            if(color[to.a] == c&&to.b == 2){
            //边是2,并且是相同颜色,说明无法二分图
                flag = 0;
                return ;
            }
            if(color[to.a] == (c^1)&&to.b == 1){
            //边是1,并且是相反 颜色,说明无法二分图
                flag = 0;
                return ;
            }
        }
    }
}

void solve()
{
    cin >> n >> m;
    init(n);
    for(int i = 1;i <= n;i++){
        a[i].clear();
        g[i].clear();
        // a[i].push_back(0);
        for(int j = 1;j <= m;j++){
            char x;cin >> x;
            a[i].push_back(x);
        }
    }   
    // cout << a[1][2] << "?";

    for(int j = 0;j <= (m-1)/2;j++){
        ll cnt1 = 0,flg=0;
        ll x,y;
        for(int i = 1;i <= n;i++){
            if(a[i][j] == '1'){
                cnt1++;
                if(cnt1 == 1)x = i;
                if(cnt1 == 2)y = i;
                if(cnt1 > 2){
                    cout << "0\n";
                    return ;
                }
            }
        }
        if(cnt1 == 2){
            flg = 1;
            edgeto xx;
            xx.a = y;
            xx.b = 2;
            g[x].push_back(xx);
            xx.a = x;
            g[y].push_back(xx);
        }
        for(int i = 1;i <= n;i++){
            if(a[i][m-j-1] == '1'){
                cnt1++;
                if(cnt1 == 1)x = i;
                if(cnt1 == 2)y = i;
                if(cnt1 > 2){
                    cout << "0\n";
                    return ;
                }
            }
        }
        if(cnt1 == 2){
            if(flg == 0){
                // if(x==y)
                edgeto xx;
                xx.a = y;
                xx.b = 1;
                g[x].push_back(xx);
                xx.a = x;
                g[y].push_back(xx);
            }
            bing(x,y);
        }
    }
    ll cnt2=0;

    color = vector<ll>(n+1,-1); //初始化为-1表示都没涂色
    for(int i = 1;i <= n;i++){
        if(fa[i] == i){
            dfs(i,1);       //用涂色法遍历整个图
        }
    }
    

    if(flag == 0){
        cout << "0\n";
        return;
    }else{
        // cout << "YES\n";
    }

    for(int i = 1;i <= n;i++){
        if(fa[i] == i){
            cnt2++;
        }
    }

    
    
    cout << quickp(2,cnt2) << "\n";
   
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3 5
01100
10001
00010
2 1
1
1
2 3
001
001

output:

4
0
2

result:

ok 3 number(s): "4 0 2"

Test #2:

score: 0
Accepted
time: 26ms
memory: 52392kb

input:

15613
10 10
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
15 8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1 5
00000
5 9
000000000
000000000
0000...

output:

1024
32768
2
32
32768
128
32
16
16
2
16384
16384
128
128
32768
8192
128
64
16384
2
4
2
4096
16
4096
1024
32768
32768
16384
8
128
2
16
4096
8192
32768
8192
8192
16
16384
16384
256
128
8
256
8
4096
512
2
4
32
32
2
64
512
1024
32768
32768
2
64
16384
16
8192
16
256
16
64
8192
8192
64
1024
2
32768
2
4
51...

result:

ok 15613 numbers

Test #3:

score: -100
Wrong Answer
time: 23ms
memory: 52444kb

input:

15759
9 6
000000
000000
000000
000000
000000
000000
000000
000000
000000
5 15
010000000000000
000000000000000
000000000000000
000100000000000
000100000000000
14 12
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000...

output:

512
16
16384
512
1024
4096
32768
4
2
512
512
512
512
8
2
256
16
4096
512
64
16
4096
512
32
32768
8192
32
2048
128
16
4096
64
32768
256
32
16384
8
512
32
2048
8
16
1024
2048
128
64
32
8
512
8
8192
256
8192
32768
2
8
512
512
256
32
2
2048
8192
8
64
8
2
16384
32768
32768
1024
4096
16384
16384
128
256
4...

result:

wrong answer 2380th numbers differ - expected: '0', found: '4'