SRM 441(1-250pt, 1-500pt)

时间:2023-03-09 00:37:20
SRM 441(1-250pt, 1-500pt)

DIV1 250pt

题意:用数组A表示置换,由该置换得到数组B(B[0] = 0, B[i] = A[B[i-1]])。给定A,求一个A',使得由A'得到的B为单循环置换且A'与A的差距最小。定义A与A'的差距为,有多少个i满足A[i] != A'[i]。返回最小差距值。A.size() <= 50。

解法:要得到的B为单循环置换,则A'也为单循环置换。如果置换A含有t个循环节,if (t==1)差距为0,否则最小差距为t,原因是可以通过交换某两个数的位置,使得两个循环变为1个循环。

tag:math, permutation

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "PerfectPermutation.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; class PerfectPermutation
{
public:
bool v[];
int reorder(vector <int> p){
clr0 (v);
int cnt = ;
for (int i = ; i < sz(p); ++ i) if (!v[p[i]]){
int t = p[i];
while (!v[t])
v[t] = , t = p[t];
++ cnt;
}
if (cnt == ) return ;
return cnt;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
void test_case_1() { int Arr0[] = {, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
void test_case_2() { int Arr0[] = {, , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
void test_case_3() { int Arr0[] = {, , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
void test_case_4() { int Arr0[] = {, , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
PerfectPermutation ___test;
___test.run_test(-);
return ;
}
// END CUT HERE

DIV1 500pt

题意:在一个无向图G中,可以做这样一种操作:

   选取一组点(A,B,C,D),其中AB有边直接连接,CD有边直接连接,AC,AD,BC,BD无直连边。那么毁坏AB,CD相连的边,并重新连接AC和BD,或者重新连接AD和BC。

   对于一张给定图,问最少多少次操作才能使其变为连通图,如果不能变为连通图输出-1。

解法:我YY了一个结论。。。如果G中已经连的边数<G中的点数-1,则输出-1;如果存在某个连通块点数为1,输出-1;否则输出连通块的数目-1。

   下面是证明:称题目所给操作为L操作。

   由题可以推出以下几点:1、L操作不改变边的数量;2、如果去除了AB相连的边,AB还是在同一个连通块中,即AB在某个环上,则L操作可以将两个连通块变为一个;3、如果某个连通块的边数>=点数,即该连通块含有环,那么一定能够通过L操作将它与另一个含有边的连通块融合为一个。而连通块含有边的条件就是点数大于1;4、对于连通块的数目,一次L操作要么不改变连通块的数目,要么使连通快的数目减1。

   由1,2,3可知,当G中边数>=点数且不存在点数为1的连通块时,可通过不断进行L操作来将将连通块数目减少到1,即此时一定可以将G变为连通图。

   由4可知,每次L操作只能使连通块数目减1,则一定需要连通块数目-1次L操作才能将G变为联通图。所以,问题得证。

Ps:官方题解貌似使用记录所有联通块的点数和边数,不断合并联通块的方式来做的。我的代码比它的简单多了^ ^。

tag:think, graph, good

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "StrangeCountry.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; class StrangeCountry
{
public:
int f[], cnt[];
int find (int x)
{
if (x != f[x]) f[x] = find(f[x]);
return f[x];
}
int transform(vector <string> g){
int num = ;
for (int i = ; i < sz(g); ++ i)
for (int j = ; j < sz(g); ++ j)
if (i != j && g[i][j] == 'Y') ++ num;
num /= ;
if (num < sz(g)-) return -; for (int i = ; i < sz(g); ++ i) f[i] = i;
for (int i = ; i < sz(g); ++ i)
for (int j = ; j < sz(g); ++ j) if (g[i][j] == 'Y'){
int t1 = find(i), t2 = find(j);
if (t1 != t2) f[t1] = t2;
}
int ret = ;
clr0 (cnt);
for (int i = ; i < sz(g); ++ i){
int t = find (i);
if (!cnt[t]) ++ ret;
++ cnt[t];
}
for (int i = ; i < sz(g); ++ i)
if (cnt[i] == ) return -;
return ret - ;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arr0[] = {"NYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYYNN", "YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYYNN", "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNN", "NNNNNNNNNNNNNNNNN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NNNNNNNNNNNNNNN", "NNNNNNNNYNNNYNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNNYNNNNNNYNNNNNNNNNNNNN", "NNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", "NNNNNYNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNNYNNNYNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNYN", "NNNNNNNNNNNNYNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNYNNNNNNNN", "NNNNNNNNNNNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNN", "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNYNNNNN", "NNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNYNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNNN", "NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNYNNNNNNNNNNNN", "NNNNNNNNNNYNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
void test_case_1() { string Arr0[] = {"NYYNN",
"YNYNN",
"YYNNN",
"NNNNY",
"NNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
void test_case_2() { string Arr0[] = {"NYYNNNN",
"YNYNNNN",
"YYNNNNN",
"NNNNYYN",
"NNNYNYY",
"NNNYYNY",
"NNNNYYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
void test_case_3() { string Arr0[] = {"NYNYNNNNNNNN",
"YNYNNNNNNNNN",
"NYNYYNNNNNNN",
"YNYNNNNNNNNN",
"NNYNNYYNNNNN",
"NNNNYNYNNNNN",
"NNNNYYNNNNNN",
"NNNNNNNNYYNN",
"NNNNNNNYNYNN",
"NNNNNNNYYNNN",
"NNNNNNNNNNNY",
"NNNNNNNNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
void test_case_4() { string Arr0[] = {"NYNNNN",
"YNYNNN",
"NYNYNN",
"NNYNNN",
"NNNNNY",
"NNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = -; verify_case(, Arg1, transform(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
StrangeCountry ___test;
___test.run_test(-);
return ;
}
// END CUT HERE