QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709131#9230. Routing K-CodesDrimpossibleWA 98ms48944kbC++144.9kb2024-11-04 12:02:072024-11-04 12:02:07

Judging History

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

  • [2024-11-04 12:02:07]
  • 评测
  • 测评结果:WA
  • 用时:98ms
  • 内存:48944kb
  • [2024-11-04 12:02:07]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

/* --------------- fast io --------------- */ // begin
namespace Fread {
	const int SIZE = 1 << 21;
	char buf[SIZE], *S, *T;
	inline char getchar() {
		if (S == T) {
			T = (S = buf) + fread(buf, 1, SIZE, stdin);
			if (S == T) return '\n';
		}
		return *S++;
	}
} // namespace Fread
namespace Fwrite {
	const int SIZE = 1 << 21;
	char buf[SIZE], *S = buf, *T = buf + SIZE;
	inline void flush() {
		fwrite(buf, 1, S - buf, stdout);
		S = buf;
	}
	inline void putchar(char c) {
		*S++ = c;
		if (S == T) flush();
	}
	struct NTR {
		~ NTR() { flush(); }
	} ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
#endif
namespace Fastio {
struct Reader {
	template<typename T>
	Reader& operator >> (T& x) {
		char c = getchar();
		T f = 1;
		while (c < '0' || c > '9') {
			if (c == '-') f = -1;
			c = getchar();
		}
		x = 0;
		while (c >= '0' && c <= '9') {
			x = x * 10 + (c - '0');
			c = getchar();
		}
		x *= f;
		return *this;
	}
	Reader& operator >> (char& c) {
		c = getchar();
		while (c == ' ' || c == '\n') c = getchar();
		return *this;
	}
	Reader& operator >> (char* str) {
		int len = 0;
		char c = getchar();
		while (c == ' ' || c == '\n') c = getchar();
		while (c != ' ' && c != '\n' && c != '\r') { // \r\n in windows
			str[len++] = c;
			c = getchar();
		}
		str[len] = '\0';
		return *this;
	}
	Reader(){}
} cin;
const char endl = '\n';
struct Writer {
	template<typename T>
	Writer& operator << (T x) {
		if (x == 0) { putchar('0'); return *this; }
		if (x < 0) { putchar('-'); x = -x; }
		static int sta[45];
		int top = 0;
		while (x) { sta[++top] = x % 10; x /= 10; }
		while (top) { putchar(sta[top] + '0'); --top; }
		return *this;
	}
	Writer& operator << (char c) {
		putchar(c);
		return *this;
	}
	Writer& operator << (char* str) {
		int cur = 0;
		while (str[cur]) putchar(str[cur++]);
		return *this;
	}
	Writer& operator << (const char* str) {
		int cur = 0;
		while (str[cur]) putchar(str[cur++]);
		return *this;
	}
	Writer(){}
} cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
/* --------------- fast io --------------- */ // end

#define int __int128
#define NO return cout<<"NIE",0

const int N=2e5+5;

int n,m,rt;
vector<int>e[N];

int dep[N],mx[2][N],cnt[N];
int f[N],g[N],mn[2][N];

void getdep(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        dep[v]=dep[u]+1;
        getdep(v,u);
        if(mx[0][v]+1>mx[0][u])
            mx[1][u]=mx[0][u],mx[0][u]=mx[0][v]+1;
        else mx[1][u]=max(mx[1][u],mx[0][v]+1);
    }
}

void getmx(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        int val=mx[0][u]+1;
        if(mx[0][v]+1==mx[0][u])val=mx[1][u]+1;
        if(val>mx[0][v])
            mx[1][v]=mx[0][v],mx[0][v]=val;
        else mx[1][v]=max(mx[1][v],val);
        getmx(v,u);
    }
}

void dfs(int u,int fa){
    f[u]=1;
    mn[0][u]=mn[1][u]=1e18;
    for(int v:e[u]){
        if(v==fa)continue;
        cnt[u]++;
        dfs(v,u);
        f[u]+=(f[v]<<1);
        g[u]+=g[v];
        if(f[v]<mn[0][u])
            mn[1][u]=mn[0][u],mn[0][u]=f[v];
        else mn[1][u]=min(mn[1][u],f[v]);
    }
    if(cnt[u]>1)g[u]+=mn[0][u];
}

void fds(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        int val=g[u]-g[v];
        if(e[u].size()>=2)
            val-=mn[0][u];
        if(e[u].size()==3){
			if(mn[0][u]==f[v])val+=mn[1][u];
        	else val += mn[0][u];
		}
        g[v]+=val;
		if(cnt[v]>1)g[v]-=mn[0][v];
        
        val=(f[u]-f[v]*2)*2;
        f[v]+=(f[u]-f[v]*2)*2;
        if(mn[0][v]==0)mn[0][v]=val;
        else if(val<mn[0][v])
            mn[1][v]=mn[0][v],mn[0][v]=val;
        else mn[1][v]=min(mn[1][v],val);
        if(e[v].size()>1)g[v]+=mn[0][v];

        fds(v,u);
    }
}

signed main(){

    cin>>n>>m;
    if(m>n-1)NO;
    for(int i=1;i<=m;i++){
        int u,v;cin>>u>>v;
        e[u].emplace_back(v);
        e[v].emplace_back(u);
    }
    for(int i=1;i<=n;i++)
        if(e[i].size()>3)NO;
    getdep(1,0),getmx(1,0);
    mx[0][0]=1e18;
    for(int i=1;i<=n;i++)
        if(mx[0][i]<mx[0][rt]&&e[i].size()<3)rt=i;
    if(mx[0][rt]>31)NO;
    dfs(rt,0);
    // for(int i=1;i<=n;i++)
    //     cout<<g[i]<<' ';
    // cout<<f[2]<<"\n";
    fds(rt,0);

    int ans=1e18;
    for(int i=1;i<=n;i++){
        if(e[i].size()==3)continue;
        if(e[i].size()==1){
            if(mx[0][i]>31)continue;
            ans=min(ans,g[i]+f[e[i][0]]-2);
        }
        else {
            if(mx[0][i]>30)continue;
            ans=min(ans,f[i]+g[i]);
        }
    }
    if(ans>=1e18)NO;
    cout<<ans;

    return 0;
}
/*
10 9
3 4
6 7
5 6
3 5
2 3
2 8
1 2
9 10
1 9
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 9956kb

input:

4 6
1 2
2 3
3 4
4 1
1 3
2 4

output:

NIE

result:

ok single line: 'NIE'

Test #3:

score: 0
Accepted
time: 0ms
memory: 9740kb

input:

10 10
9 3
5 10
1 4
10 8
8 3
7 3
9 6
8 6
7 2
4 5

output:

NIE

result:

ok single line: 'NIE'

Test #4:

score: 0
Accepted
time: 54ms
memory: 32488kb

input:

200000 199999
172774 188052
195063 155577
38023 148303
30605 155047
170238 19344
46835 58255
154268 109062
197059 116041
136424 12058
42062 149807
109545 115380
132322 51106
16706 162612
62234 31319
195735 80435
173898 84051
19876 102910
186924 9136
123094 117097
145054 173049
137364 152731
82662 18...

output:

NIE

result:

ok single line: 'NIE'

Test #5:

score: -100
Wrong Answer
time: 98ms
memory: 48944kb

input:

200000 199999
168192 30733
164413 46673
175210 2329
69389 67102
33991 152553
91061 55265
166724 117726
90148 157176
34831 12213
60756 105488
121891 155192
82202 155666
102083 156661
38968 75200
190004 107321
72436 92732
64314 65004
39210 91106
70455 173568
179742 29844
126232 19552
133509 110602
131...

output:

20980174962373

result:

wrong answer 1st lines differ - expected: '20980030044349', found: '20980174962373'