QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#287146 | #7854. 这不是一道数据结构题 | chenxinyang2006 | 25 | 9ms | 37608kb | C++14 | 6.9kb | 2023-12-19 20:02:54 | 2023-12-19 20:02:55 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template <class T>
void chkmax(T &x,T y){
if(x < y) x = y;
}
template <class T>
void chkmin(T &x,T y){
if(x > y) x = y;
}
inline int popcnt(int x){
return __builtin_popcount(x);
}
inline int ctz(int x){
return __builtin_ctz(x);
}
template <int P>
class mod_int
{
using Z = mod_int;
private:
static int mo(int x) { return x < 0 ? x + P : x; }
public:
int x;
int val() const { return x; }
mod_int() : x(0) {}
template <class T>
mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
bool operator==(const Z &rhs) const { return x == rhs.x; }
bool operator!=(const Z &rhs) const { return x != rhs.x; }
Z operator-() const { return Z(x ? P - x : 0); }
Z pow(long long k) const
{
Z res = 1, t = *this;
while (k)
{
if (k & 1)
res *= t;
if (k >>= 1)
t *= t;
}
return res;
}
Z &operator++()
{
x < P - 1 ? ++x : x = 0;
return *this;
}
Z &operator--()
{
x ? --x : x = P - 1;
return *this;
}
Z operator++(int)
{
Z ret = x;
x < P - 1 ? ++x : x = 0;
return ret;
}
Z operator--(int)
{
Z ret = x;
x ? --x : x = P - 1;
return ret;
}
Z inv() const { return pow(P - 2); }
Z &operator+=(const Z &rhs)
{
(x += rhs.x) >= P && (x -= P);
return *this;
}
Z &operator-=(const Z &rhs)
{
(x -= rhs.x) < 0 && (x += P);
return *this;
}
Z operator-()
{
return -x;
}
Z &operator*=(const Z &rhs)
{
x = 1ULL * x * rhs.x % P;
return *this;
}
Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o) \
friend T operator o(const Z &lhs, const Z &rhs) \
{ \
Z res = lhs; \
return res o## = rhs; \
}
setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
friend istream& operator>>(istream& is, mod_int& x)
{
long long tmp;
is >> tmp;
x = tmp;
return is;
}
friend ostream& operator<<(ostream& os, const mod_int& x)
{
os << x.val();
return os;
}
};
using Z = mod_int<mod>;
Z power(Z p,ll k){
Z ans = 1;
while(k){
if(k % 2 == 1) ans *= p;
p *= p;
k /= 2;
}
return ans;
}
int n,m;
int _u[200005],_v[200005];
Z fact[200005];
int cnt;
int head[200005];
struct eg{
int to,nxt;
}edge[400005];
void make(int u,int v){
edge[++cnt].to = v;
edge[cnt].nxt = head[u];
head[u] = cnt;
}
int dfn[200005],low[200005],N;
vector <int> sta,scc[200005],idx[200005];
void tarjan(int u){
dfn[u] = low[u] = ++cnt;
sta.eb(u);
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(!dfn[v]){
tarjan(v);
chkmin(low[u],low[v]);
if(low[v] == dfn[u]){
++N;
scc[N].eb(u);
while(1){
int p = sta.back();
sta.pop_back();
scc[N].eb(p);
if(p == v) break;
}
}
}else{
chkmin(low[u],dfn[v]);
}
}
}
vector <int> son[400005];
int anc[400005];
void dfs(int u,int f){
anc[u] = f;
for(int v:son[u]) if(v != f) dfs(v,u);
}
queue <int> Q;
int deg[200005],qwq[200005];
map <int,int> G[200005];
void add(int u,int v){
// printf("add %d %d\n",u,v);
qwq[u]++;qwq[v]++;
make(u,v);
make(v,u);
}
void test(int u){
deg[u]--;
if(deg[u] == 2) Q.push(u);
}
int vis[200005];
void dfs2(int u){
vis[u] = 1;
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(!vis[v]) dfs2(v);
}
}
int rk[200005];
int main(){
// freopen("test.in","r",stdin);
scanf("%d%d",&n,&m);
fact[0] = 1;
rep(i,1,n) fact[i] = fact[i - 1] * i;
rep(i,1,m){
scanf("%d%d",&_u[i],&_v[i]);
make(_u[i],_v[i]);make(_v[i],_u[i]);
}
cnt = 0;
tarjan(1);
rep(c,1,N){
for(int u:scc[c]){
son[n + c].eb(u);
son[u].eb(n + c);
}
}
dfs(1,0);
rep(i,1,m){
if(anc[_u[i]] == anc[_v[i]]){
idx[anc[_u[i]]].eb(i);
continue;
}
if(anc[anc[_u[i]]] == _v[i]) idx[anc[_u[i]]].eb(i);
else if(anc[anc[_v[i]]] == _u[i]) idx[anc[_v[i]]].eb(i);
else assert(0);
}
// printf("qwq\n");
Z ans = n;
cnt = 0;
memset(head,0,sizeof(head));
rep(c,1,N){
/* printf("vcc %d\n",c);
for(int id:idx[n + c]) printf("%d ",id);
printf("\n");
for(int u:scc[c]) printf("%d ",u);
printf("\n");*/
if(SZ(scc[c]) == 2){
add(_u[idx[n + c].back()],_v[idx[n + c].back()]);
qwq[_u[idx[n + c].back()]] = qwq[_v[idx[n + c].back()]] = 0;
continue;
}
ans *= 2;
for(int id:idx[n + c]){
G[_u[id]][_v[id]] = 1;
G[_v[id]][_u[id]] = 1;
deg[_u[id]]++;deg[_v[id]]++;
}
for(int u:scc[c]) if(deg[u] == 2) Q.push(u);
rep(k,1,SZ(scc[c]) - 2){
if(Q.empty()){
printf("0\n");
return 0;
}
int u = Q.front();
Q.pop();
int p = -1,q = -1;
for(pii I:G[u]){
if(!I.second) continue;
if(p == -1) p = I.first;
else if(q == -1) q = I.first;
else assert(0);
if(I.second == 1) add(u,I.first);
}
// printf("erase %d p=%d q=%d\n",u,p,q);
if(q == -1){
printf("0\n");
return 0;
}
G[p][u] = G[q][u] = 0;
G[u].clear();
if(G[p].find(q) != G[p].end() && G[p][q]){
test(p);
test(q);
}
G[p][q] = G[q][p] = 2;
}
if(SZ(Q) < 2){
printf("0\n");
return 0;
}
assert(SZ(Q) == 2);
int p = Q.front();Q.pop();
int q = Q.front();Q.pop();
if(G[p][q] == 1) add(p,q);
G[p].clear();G[q].clear();
for(int u:scc[c]){
if(qwq[u] != 2){
printf("0\n");
return 0;
}
}
}
dfs2(1);
rep(u,1,n){
if(!vis[u]){
printf("0\n");
return 0;
}
}
rep(c,1,N){
for(int u:scc[c]) rk[u]++;
}
rep(u,1,n) ans *= fact[rk[u]];
printf("%d\n",ans.val());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 34432kb
input:
7 9 1 2 2 3 3 4 1 5 2 5 2 6 5 6 5 7 6 7
output:
56
result:
ok single line: '56'
Test #2:
score: 0
Accepted
time: 3ms
memory: 35140kb
input:
10 14 10 9 4 1 7 10 3 5 9 6 1 7 8 5 2 7 8 6 4 10 9 3 8 7 4 5 7 3
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 34496kb
input:
10 14 7 9 5 1 2 1 8 3 6 5 4 2 10 6 8 1 7 10 7 1 2 8 3 4 6 3 9 1
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 36040kb
input:
10 15 4 9 6 8 6 1 3 2 3 10 2 7 3 6 1 9 9 5 1 8 3 8 10 6 5 8 3 7 7 10
output:
40
result:
ok single line: '40'
Subtask #2:
score: 0
Runtime Error
Test #5:
score: 0
Runtime Error
input:
200000 199999 76849 117660 190775 11517 36929 136177 161792 186900 165326 184615 74197 120051 7891 83240 121896 35204 83314 195652 104144 158348 71191 182187 122824 50031 108950 179173 165120 190230 156949 181392 171984 82834 96017 30143 58114 108979 38698 90371 185399 171751 139913 99465 71566 1324...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 9ms
memory: 34592kb
input:
300 305 289 290 34 35 111 140 90 93 233 240 110 271 116 117 12 21 141 142 53 57 21 85 99 102 34 42 183 184 240 264 252 253 223 224 159 160 126 131 112 113 28 33 50 52 204 208 185 188 46 50 262 264 197 199 111 136 259 261 290 294 49 50 263 264 210 212 291 294 203 208 184 185 120 121 111 131 210 240 2...
output:
0
result:
wrong answer 1st lines differ - expected: '482487615', found: '0'
Subtask #5:
score: 0
Wrong Answer
Test #18:
score: 0
Wrong Answer
time: 3ms
memory: 37404kb
input:
300 500 279 256 263 65 40 62 236 256 8 193 164 235 242 256 27 219 72 244 49 253 289 261 162 113 196 199 121 222 293 245 33 186 206 279 111 139 97 15 203 24 245 157 184 59 188 90 239 283 42 5 107 108 267 51 200 126 286 282 293 59 42 261 276 216 152 6 92 220 225 69 88 166 179 109 158 144 133 18 147 18...
output:
0
result:
wrong answer 1st lines differ - expected: '159215763', found: '0'
Subtask #6:
score: 20
Accepted
Test #23:
score: 20
Accepted
time: 7ms
memory: 37608kb
input:
300 597 181 11 16 186 144 42 80 274 72 186 238 172 7 268 225 118 198 84 274 214 170 27 181 44 171 74 270 266 20 6 296 108 45 25 32 198 99 86 129 110 281 273 67 47 259 107 277 265 264 145 215 218 264 164 156 281 100 23 284 125 109 280 92 203 108 74 227 171 213 81 262 239 206 111 5 23 90 121 77 274 23...
output:
600
result:
ok single line: '600'
Test #24:
score: 0
Accepted
time: 3ms
memory: 36000kb
input:
300 597 145 168 81 149 192 248 114 243 113 25 201 259 190 156 225 40 127 199 225 33 229 4 298 36 243 81 31 163 192 294 258 31 8 299 210 28 87 143 180 205 270 281 257 174 173 187 203 128 179 292 34 4 292 236 288 248 78 255 7 239 191 124 227 194 113 187 270 93 164 238 256 37 121 88 279 133 11 99 189 1...
output:
600
result:
ok single line: '600'
Test #25:
score: 0
Accepted
time: 7ms
memory: 35448kb
input:
300 597 137 70 124 146 24 239 38 218 186 180 244 153 274 153 274 298 256 126 36 174 172 157 60 274 270 87 207 14 114 280 221 51 165 106 166 25 29 62 210 122 104 157 218 173 134 154 65 120 131 46 45 262 138 253 48 187 118 114 101 184 47 244 200 60 296 267 34 128 129 165 53 246 102 123 153 238 238 249...
output:
600
result:
ok single line: '600'
Test #26:
score: 0
Accepted
time: 3ms
memory: 37384kb
input:
300 597 279 221 253 68 255 267 293 234 88 82 272 133 157 107 129 78 221 160 45 205 8 197 295 127 212 288 232 224 274 23 101 65 4 272 112 300 51 225 221 236 213 236 166 182 87 57 236 254 295 259 28 128 124 39 157 177 133 49 64 45 291 94 161 111 98 144 244 92 135 64 39 44 171 244 241 238 268 296 208 2...
output:
0
result:
ok single line: '0'
Test #27:
score: 0
Accepted
time: 3ms
memory: 35452kb
input:
300 597 240 186 292 198 221 201 168 95 237 175 71 272 55 257 181 281 47 115 188 83 251 142 297 77 271 181 68 75 267 209 195 92 22 54 134 300 253 251 238 13 179 199 246 144 61 199 65 271 113 46 180 204 144 209 84 184 124 250 238 212 70 30 118 125 175 39 88 54 253 159 208 243 115 262 30 128 214 22 187...
output:
0
result:
ok single line: '0'
Test #28:
score: 0
Accepted
time: 3ms
memory: 34144kb
input:
300 597 166 131 24 224 206 54 208 109 243 157 191 268 162 109 260 285 238 144 193 283 57 12 145 76 186 192 24 72 96 243 80 137 144 127 59 87 50 106 179 170 93 261 249 66 160 210 177 239 183 287 226 249 115 195 252 153 289 284 93 144 280 227 289 170 17 210 14 217 154 54 149 177 138 88 294 230 211 93 ...
output:
0
result:
ok single line: '0'
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #3:
0%