QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#646248#6613. Bitwise Exclusive-OR Sequencewzxtsl#TL 4ms5676kbC++234.0kb2024-10-16 21:55:102024-10-16 21:55:11

Judging History

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

  • [2024-10-16 21:55:11]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:5676kb
  • [2024-10-16 21:55:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define For(i,a,aa) for(int i=a;i<=aa;i++)
const int N=1e5+50;
int n,m,x,y,w;
int d[N][40];
int yi[40],lin[40];
int p[40],cnt[40];
vector <int> v[N][40];
int mp[N][40];
void solve(){
    int ans=0;
	cin>>n>>m;
    For(i,1,n)
        For(j,1,30)
            d[i][j]=-1;
    For(j,1,30) cnt[j]=0;
    For(i,1,m)
    {
        cin>>x>>y>>w;
        int a[40],tot=0;
        memset(a,0,sizeof(a));
        while(w)
        {
            a[++tot]=w%2;
            w>>=1;
        }
        For(j,tot+1,30) a[j]=0;
        For(j,1,30)
        {
            if(d[x][j]!=-1&&d[y][j]!=-1)
            {
                if(d[x][j]^d[y][j]==0&&a[j]==1) 
                {
                    cout<<-1;return;
                }
                else if(d[x][j]^d[y][j]==1&&a[j]==0) 
                {
                    cout<<-1;return;
                }     
                else 
                {
                    //x,x^1,y,y^1合并
                    if((v[d[y][j]][j].size()+v[d[y][j]^1][j].size())<=(v[d[x][j]][j].size()+v[d[x][j]^1][j].size()))
                    {
                        For(k,0,v[d[y][j]][j].size()-1) 
                            v[a[j]^d[x][j]][j].push_back(v[d[y][j]][j][k]),d[v[d[y][j]][j][k]][j]=d[x][j];
                        v[d[y][j]][j].clear();
                        For(k,0,v[d[y][j]^1][j].size()-1) 
                            v[a[j]^(d[x][j]^1)][j].push_back(v[d[y][j]^1][j][k]),d[v[d[y][j]^1][j][k]][j]=d[x][j]^1;
                        v[d[y][j]^1][j].clear();                         
                    }
                    else{
                        For(k,0,v[d[x][j]][j].size()-1) 
                            v[a[j]^d[y][j]][j].push_back(v[d[x][j]][j][k]),d[v[d[x][j]][j][k]][j]=d[y][j];
                        v[d[x][j]][j].clear();
                        For(k,0,v[d[x][j]^1][j].size()-1) 
                            v[a[j]^(d[y][j]^1)][j].push_back(v[d[x][j]^1][j][k]),d[v[d[x][j]^1][j][k]][j]=d[y][j]^1;
                        v[d[x][j]^1][j].clear();                         
                    }
                }
            }
            else if(d[y][j]==-1&&d[x][j]==-1)
            {
                if(a[j])
                {
                    v[cnt[j]][j].push_back(x);
                    v[cnt[j]+1][j].push_back(y);
                    d[x][j]=cnt[j];
                    d[y][j]=cnt[j]+1;    
                    cnt[j]+=2;                      
                }
                else{
                    v[cnt[j]][j].push_back(x);
                    v[cnt[j]][j].push_back(y);
                    d[x][j]=cnt[j];
                    d[y][j]=cnt[j];    
                    cnt[j]+=2;                     
                }
            }
            else if(d[x][j]==-1)
            {
                if(a[j])
                {
                    d[x][j]=d[y][j]^1;
                    v[d[x][j]][j].push_back(x);                    
                }
                else{
                    d[x][j]=d[y][j];
                    v[d[x][j]][j].push_back(x);                      
                }
            }
            else{
                if(a[j])
                {
                    d[y][j]=d[x][j]^1;
                    v[d[y][j]][j].push_back(y);                    
                }
                else
                {
                    d[y][j]=d[x][j];
                    v[d[y][j]][j].push_back(y);                      
                }
            }
        }
    }
	For(j,1,30)
    {
        int tp=0;
        for(int i=0;i<cnt[j];i+=2)
        {
            tp+=min(v[i][j].size(),v[i+1][j].size());//cout<<v[i][j].size()<<"!"<<v[i+1][j].size()<<"!!"<<endl;
        }
        ans+=p[j]*tp;//cout<<ans<<"!";
        //cout<<cnt[j]<<"!";
    }
    cout<<ans;
}
signed main(){
	fast;
	int t=1;
	//cin>>t;
    p[1]=1;
    For(i,2,30)    p[i]=p[i-1]*2;
	while(t--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 5624kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 4ms
memory: 5676kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Time Limit Exceeded

input:

5 5
3 4 12
3 1 20
2 5 16
1 4 24
4 5 19

output:


result: