#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> usingnamespace std; #define MAXN 100006 int n , k; int A[MAXN] , cnt[MAXN]; intksm( int a , int x ){ longlong ans = 1 , cur = a; while( x ) { if( x & 1 ) ans *= cur; cur *= cur , x >>= 1; if( ans > n || cur > n ) return0x3f3f3f3f; } return ans; } vector<int> group[MAXN]; int ok[MAXN] , vis[MAXN]; boolned( int x , int w ){ if( x == n + 1 ) return ok[x] = false; if( ~ok[x] ) return ok[x]; if( cnt[x] ) { group[w].push_back( x ) , --cnt[x]; returntrue; } int r = k; while( r-- ) if( !ned( x + 1 , w ) ) return ok[x] = false; returntrue; } vector<int> pos[MAXN];int t[MAXN]; intmain(){ int T;cin >> T; while( T-- ) { cin >> n >> k; memset( ok , -1 , sizeof ok ); memset( vis , 0 , sizeof vis ); memset( cnt , 0 , sizeof cnt ); memset( t , 0 , sizeof t ); for( int i = 1 ; i <= n ; ++ i ) pos[i].clear(); for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&A[i]) , ++cnt[A[i]] , pos[A[i]].push_back( i ); for( int i = 0 ; i < k ; ++ i ) group[i].clear(); int flg = 0; int r = k; while( r-- ) { if( !ned( 1 , r ) ) { puts( "0" ); flg = 1 ; break; } } if( flg ) continue; int poi = 0; printf("1"); for( int i = 0 ; i < k ; ++ i ) { puts(""); printf("%d ",i != k - 1 ? group[i].size() : n - poi); poi += group[i].size(); for( int j = 0 ; j < group[i].size() ; ++ j ) printf("%d ",pos[group[i][j]][t[group[i][j]]]) , vis[pos[group[i][j]][t[group[i][j]]]] = 1 , ++ t[group[i][j]]; } for( int i = 1 ; i <= n ; ++ i ) if( !vis[i] ) printf("%d ",i); puts(""); } }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<cmath> #include<queue> #define inf 0x3f3f3f3f usingnamespace std; #define MAXN 10100 int n , m , s , t , q1 , q2; structedge{ int to , w , dis , nxt; }e[MAXN<<3]; int cnt = -1; int maxf = 0 , minc = 0 , fl[MAXN] , dis[MAXN] , pre[MAXN] ; int head[MAXN] , lst[MAXN]; voidade( int u , int v , int w , int d ){ e[++cnt].nxt = head[u] , e[cnt].w = w; e[cnt].to = v , e[cnt].dis = d; head[u] = cnt; e[++cnt].nxt = head[v] , e[cnt].w = 0; e[cnt].to = u , e[cnt].dis = -d; head[v] = cnt; } voidsfpa( ); int hd1[MAXN<<1] , nex1[MAXN<<1] , to1[MAXN<<1] , rt1 , cnt1 = 0; int hd2[MAXN<<1] , nex2[MAXN<<1] , to2[MAXN<<1] , rt2 , cnt2 = 0; int lim1[MAXN] , lim2[MAXN]; voidade1( int u , int v ){ to1[++cnt1] = v , nex1[cnt1] = hd1[u] , hd1[u] = cnt1; } voidade2( int u , int v ){ to2[++cnt2] = v , nex2[cnt2] = hd2[u] , hd2[u] = cnt2; } int fa1[MAXN] , fa2[MAXN]; voiddfs1( int u , int fa ){ for( int i = hd1[u] ; i ; i = nex1[i] ) { int v = to1[i]; if( v == fa ) continue; fa1[v] = u; dfs1( v , u ); } } voiddfs2( int u , int fa ){ for( int i = hd2[u] ; i ; i = nex2[i] ) { int v = to2[i]; if( v == fa ) continue; fa2[v] = u; dfs2( v , u ); } } int c[MAXN]; intmain(){ memset( head , -1 , sizeof head ); cin >> n; s = 3 * n + 1 , t = 3 * n + 2; for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&c[i]); cin >> rt1; for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v); ade1( u , v ) , ade1( v , u ); } cin >> rt2; for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v); ade2( u , v ) , ade2( v , u ); } memset( lim1 , 0x7f , sizeof lim1 ) , memset( lim2 , 0x7f , sizeof lim2 ); cin >> q1; for( int i = 1 , u , x ; i <= q1 ; ++ i ) { scanf("%d%d",&u,&x); lim1[u] = x; } cin >> q2; for( int i = 1 , u , x ; i <= q2 ; ++ i ) { scanf("%d%d",&u,&x); lim2[u] = x; } dfs1( rt1 , rt1 ) , dfs2( rt2 , rt2 ); fa1[rt1] = t , fa2[rt2] = t - n; for( int i = 1 ; i <= n ; ++ i ) { ade( i , fa1[i] , lim1[i] , 0 ); ade( i + n , fa2[i] + n , lim2[i] , 0 ); ade( i + 2 * n , i , 1 , 0 ) , ade( i + 2 * n , i + n , 1 , 0 ); ade( s , i + 2 * n , 1 , -c[i] ); } sfpa( ); cout << - minc << endl; } boolspfa( int s , int t ); voidsfpa( ){ while( spfa( s , t ) ) { int u = t; maxf += fl[t] , minc += fl[t] * dis[t]; while( u != s ) { e[lst[u]].w -= fl[t] , e[lst[u] ^ 1].w += fl[t]; u = pre[u]; } } } bool vis[MAXN]; queue<int> Q; boolspfa( int s , int t ){ memset( dis , 0x7f , sizeof dis ); memset( fl , 0x7f , sizeof fl ); memset( vis , 0 , sizeof vis ); memset( pre , -1 , sizeof pre ); Q.push( s ) , vis[s] = 1 , dis[s] = 0; while( !Q.empty() ) { int u = Q.front(); Q.pop(); vis[u] = 0; for( int i = head[u] ; ~i ; i = e[i].nxt ) { if( e[i].w > 0 && dis[e[i].to] > dis[u] + e[i].dis ) { dis[e[i].to] = dis[u] + e[i].dis; pre[e[i].to] = u; lst[e[i].to] = i; fl[e[i].to] = min( fl[u] , e[i].w ); if( !vis[e[i].to] ) vis[e[i].to] = true , Q.push( e[i].to ); } } } return pre[t] != -1; }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<unordered_map> #include<vector> usingnamespace std; #define MAXN 200006 #define int long long int n , m; longlong A[MAXN];
unordered_map<longlong , int> bac;
structedge{ int u , v , w , id; } E[MAXN] ; boolcmp( edge a , edge b ){ return a.w < b.w; }
vector<int> G[MAXN] , W[MAXN]; int fa[MAXN]; intfind( int x ){ return x == fa[x] ? x : fa[x] = find( fa[x] ); }
longlong Sw[MAXN] , Sl[MAXN]; int siz[MAXN] , wtf[MAXN]; // 点权和 和 链权和 voiddfs( int u , int fa ){ Sw[u] = A[u] , siz[u] = 1; for( int i = 0 ; i < G[u].size() ; ++ i ) { int v = G[u][i] , w = W[u][i]; if( v == fa ) continue; Sl[u] += w , wtf[v] = w; dfs( v , u ); siz[u] += siz[v] , Sl[u] += Sl[v] , Sw[u] += Sw[v]; } } int w[MAXN]; boolcmpp( int a , int b ){ return w[a] > w[b]; } boolcmm( int a , int b ){ return a > b; } voidwork( int u , int fa ){ for( int i = 0 ; i < G[u].size() ; ++ i ) { int v = G[u][i] , ww = W[u][i]; if( v == fa ) { w[fa] = 0 , W[u][i] = 0; continue; } w[v] = Sw[v] - Sl[v] - ww , W[u][i] = Sw[v] - Sl[v] - ww; } sort( G[u].begin() , G[u].end() , cmpp ) , sort( W[u].begin() , W[u].end() , cmm ); int flg = 0; if( - wtf[u] + A[fa] >= 0 && u != 1 ) printf("%lld ",bac[ 1ll * min( u , fa ) * MAXN + max( u , fa ) ]) , flg = 1 , A[u] += A[fa] - wtf[u]; for( int i = 0 ; i < G[u].size() ; ++ i ) { int v = G[u][i]; if( v == fa ) continue; work( v , u ) , A[u] = A[u] + W[u][i]; } if( !flg && u != 1 ) printf("%lld ",bac[ 1ll * min( u , fa ) * MAXN + max( u , fa ) ]) , A[u] += A[fa] - wtf[u]; }
signedmain( ){ cin >> n >> m; longlong Re = 0; for( int i = 1 ; i <= n ; ++ i ) scanf("%lld",&A[i]) , Re += A[i]; for( int i = 1 ; i <= m ; ++ i ) { scanf("%lld%lld%lld",&E[i].u,&E[i].v,&E[i].w) , E[i].id = i; if( E[i].u > E[i].v ) swap( E[i].u , E[i].v ); } sort( E + 1 , E + 1 + m , cmp ); for( int i = 1 ; i <= n ; ++ i ) fa[i] = i; longlong re = 0; for( int i = 1 ; i <= m ; ++ i ) { int u = E[i].u , v = E[i].v; if( find( u ) != find( v ) ) { fa[find( u )] = find( v ); bac[ 1ll * u * MAXN + v ] = E[i].id; G[u].push_back( v ) , G[v].push_back( u ); W[u].push_back( E[i].w ) , W[v].push_back( E[i].w ); re += E[i].w; } } if( Re < re ) returnputs("0") , 0; puts("1"); dfs( 1 , 1 ); work( 1 , 1 ); }
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> usingnamespace std; #define MAXN 200006 int w , h , n;
intgetpos( int ww , int hh ){ // 是哪一个求会落到 ww , hh hh %= 2 * w; int flg = 0; if( hh >= w ) flg = 1; hh %= w ; ++ hh; int t = hh - ww + 1 , res = 0; if( t <= 0 ) res = 1 , t = -t , t += 2; if( t & 1 ) { if( t == 1 ) res += 1; else res += t - 1; } else { res = 0; t = hh - ( w - ww ); if( t <= 0 ) res = -1 , t = -t , t += 2; if( t & 1 ) { if( t == 1 ) res += w; else res += w + 2 - t; } } if( flg ) res = w + 1 - res; return res; } int ans[MAXN] , pre[MAXN]; structopt{ int x , y; } op[MAXN] ; boolcmp( opt a , opt b ){ return a.y > b.y; } intmain(){ cin >> h >> w >> n; for( int i = 1 ; i <= w ; ++ i ) pre[i] = getpos( i , h ) , ans[pre[i]] = i; for( int i = 1 ; i <= n ; ++ i ) scanf("%d%d",&op[i].y,&op[i].x); sort( op + 1 , op + 1 + n , cmp ); for( int i = 1 ; i <= n ; ++ i ) { int l = op[i].x , r = op[i].x + 1 , ht = op[i].y; swap( ans[getpos( l , ht )] , ans[getpos( r , ht )] ); } for( int i = 1 ; i <= w ; ++ i ) printf("%d\n",ans[i]); }
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> usingnamespace std; typedeflonglong ll; #define MAXN 27 #define int long long int n; ll c[MAXN]; ll mx = 0; intwork( int a , int x , int c ){ return c - a * ( x - 1 ) >= 0 ? ( x - 2 ) * a + min( a , c - ( x - 1 ) * a ) : 0; } signedmain(){ cin >> n; for( int i = 1 ; i <= n ; ++i ) scanf("%lld",&c[i]) , mx = max( mx , c[i] ); ll res = 0; for( ll k = 2 , r , s ; k <= mx + 1 ; k = r + 1 ) { // 这里其实是 k + 1 r = 0x3f3f3f3f3f3f3f3f , s = 0; for( int i = 1 ; i <= n ; ++ i ) { int a = c[i] / ( k ); s += max( work( a , k , c[i] ) , work( a + 1 , k , c[i] ) ); if( c[i] / k ) r = min( r , c[i] / ( c[i] / k ) ); } res = max( res , s ); } cout << res << endl; }
ll head[MAXN] , to[MAXN] , nex[MAXN] , cnt; voidade( int u , int v ){ to[++cnt] = v , nex[cnt] = head[u] , head[u] = cnt; } ll n,siz[MAXN],sum[MAXN]; ll c[MAXN]; ll res; //siz 为 子树 大小 sum 为当前处理过 的 所有 颜色为 c 的 点的 子树 上 的点的个数 //res 为一个块里面的 选两个点 的个数 voiddfs(ll u,ll fa){ siz[u] = 1; ll last = sum[c[u]]; for( int i = head[u] ; i ; i = nex[i] ) { ll v = to[i]; if( v == fa ) continue; ll cur = sum[c[u]]; dfs(v,u); siz[u] += siz[v]; ll sz = siz[v] - sum[c[u]] + cur; sz %= P; res -= sz*(sz-1)/2 , res += P , res %= P; } sum[c[u]] = last + siz[u] , sum[c[u]] %= P; } ll J[MAXN]; intmain(){ res = 0; cin >> n; J[0] = 1;for( int i = 1 ; i <= n ; ++ i ) J[i] = J[ i - 1 ] * i , J[i] %= P; for (ll i = 1; i <= n; ++i) scanf("%lld", &c[i]); for (ll i = 0; i < n - 1; ++i) { static ll u, v; scanf("%lld%lld", &u, &v); ade( u , v ) , ade( v , u ); } dfs(1, 1); for (ll i = 1; i <= n; ++i) if (sum[i]) res += n * (n - 1) / 2 , res -= (n - sum[i]) * (n - sum[i] - 1) / 2 , res %= P; printf("%lld\n", res % P * 2 % P * J[n - 1] % P ); }