QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325958#6315. 填数游戏littlecat#Compile Error//C++143.3kb2024-02-12 05:48:262024-07-04 03:23:45

Judging History

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

  • [2024-07-04 03:23:45]
  • 评测
  • [2024-02-12 05:48:26]
  • 提交

answer

//#define DEBUG
#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
using namespace std; const int mx=1000000; typedef pair<int,int> pi;
#define pb push_back
#define f first
#define s second

pi s1[mx],s2[mx]; bool sz1[mx],edge[mx]; vector<int> adj0[mx]; //initial sets
pi e[mx],es[mx]; vector<int> adj[mx]; //graph

int cntE,cntV; bool vis[mx]; //components
void dfs1(int c)
{
    if(vis[c]) return; cntV++, cntE+=adj[c].size(), vis[c]=1;
    for(int t:adj[c]) dfs1(e[t].f+e[t].s-c);
}

bool cycle[mx],cpar[mx]; int rt;
void dfs2(int c, int p)
{
    cpar[c]=p; for(int t:adj[c])
    {
        int k=e[t].f+e[t].s-c; if(k==p) continue; if(cpar[k]==-1) dfs2(k,c);
        else {rt=c; while(1) {cycle[rt]=1; if(rt==k) break; rt=cpar[rt];}}
    }
}
bool vis3[mx]; int cnt3;
void dfs3(int c, int p)
{
    if(vis3[c]) return; vis3[c]=1; for(int t:adj[c])
    {
        int k=e[t].f+e[t].s-c; if(k==p) continue; dfs3(k);
        if(!cycle[k]) cnt3+=k==es[t].f||k==es[t].s;
    }
}
vector<int> cv, cn;
void getcycle(int c)
{
    cn.pb(c); if(cc==rt&&!cv.empty()) return; for(int t:adj[c])
    {
        int k=e[t].f+e[t].s-c;
        if(cycle[k]&&!(!cv.empty()&&t==cv.back())) cv.pb(t), getcycle(k);
    }
}

int solve()
{
#ifdef DEBUG
cout<<"Start\n"<<flush;
#endif
    //note: swap m/n
    int m,n,a,b,ans=0; cin>>m>>n;
    for(int i=0; i<n; i++) adj0[i].clear(), adj[i].clear(), vis[i]=0, cycle[i]=0, cpar[i]=-1;
    for(int i=0; i<m; i++) edge[i]=1;
    //input and delete |T|=1
{
    for(int i=0; i<m; i++) {int c; cin>>c>>a; if(c==2) cin>>b; else b=0; s1[i]=pi(a-1,b-1);}
    for(int i=0; i<m; i++) {int c; cin>>c>>a; if(c==2) cin>>b; else b=0; s2[i]=pi(a-1,b-1);
        sz1[i]=2-c, adj0[a-1].pb(i); if(c==2) adj0[b-1].pb(i);}
    vector<int> rm; for(int i=0; i<m; i++) if(sz1[i]) rm.pb(i);
    while(!rm.empty())
    {
        int i=rm.back(); rm.pop_back(); if(!edge[i]) continue; if(s2[i].f==-1) return -1;
        ans+=s2[i].f==s1[i].f||s2[i].f==s1[i].s;
#ifdef DEBUG
cout<<"Assign "<<i<<' '<<s2[i].f<<" ("<<s2[i].s<<")\n";
#endif
        edge[i]=0; for(int j:adj0[s2[i].f]) {if(s2[j].f==s2[i].f) s2[j].f=s2[j].s; s2[j].s=-1, rm.pb(j);}
    }
    int E=0;
    for(int i=0; i<m; i++) if(edge[i]) adj[s2[i].f].pb(E),adj[s2[i].s].pb(E), e[E]=s2[i],es[E++]=s1[i];
    m=E;
}
#ifdef DEBUG
cout<<m<<' '<<ans<<'\n'; for(int i=0; i<m; i++) cout<<e[i].f<<' '<<e[i].s<<' '<<es[i].f<<' '<<es[i].s<<'\n';
for(int i=0; i<n; i++) {cout<<i<<'\t'; for(int t:adj[i]) cout<<t<<' '; cout<<'\n';} cout<<flush;
#endif
for(int i=0; i<n; i++) if(!vis[i])
{
    //count edges
    cntE=0, cntV=0, dfs1(i); if(cntE>2*cntV) return -1;
    if(cntE==2*cntV)
    {
        //cycle with trees: explicit formula
        dfs2(i,-2);
        cnt3=0, dfs3(rt,-1), ans+=cnt3;
        cv.clear(), cn.clear(), getcycle(rt);
        int A=0,B=0,C=0; for(int i=0; i<cn.size(); i++)
        {
            bool x1=cn[i]==e[cv[i]].f||cn[i]==e[cv[i]].s, x2=cn[i+1]==e[cv[i+1]].f||cn[i]==e[cv[i]].s;
            if(x1&&x2) C++; else if(x1) A++; else if(x2) B++;
        }
        ans+=min(min(A,B)+C,(A+B+C)/2);
    }
    else
    {
        //tree with variable root: dp
        throw(0);
    }
}
    return ans;
}
int main()
{
    int T; cin.sync_with_stdio(0),cin.tie(0),cin>>T; while(T--) cout<<solve()<<'\n';
}

Details

answer.code: In function ‘void dfs3(int, int)’:
answer.code:34:55: error: too few arguments to function ‘void dfs3(int, int)’
   34 |         int k=e[t].f+e[t].s-c; if(k==p) continue; dfs3(k);
      |                                                   ~~~~^~~
answer.code:30:6: note: declared here
   30 | void dfs3(int c, int p)
      |      ^~~~
answer.code: In function ‘void getcycle(int)’:
answer.code:41:18: error: ‘cc’ was not declared in this scope; did you mean ‘c’?
   41 |     cn.pb(c); if(cc==rt&&!cv.empty()) return; for(int t:adj[c])
      |                  ^~
      |                  c