This time rank23~ back to the purple name ~
A. Enumerate the inserted positions and the inserted characters, and the brute force can be judged.
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include < algorithm> #include <cstring> #include <string>using namespace std;string s;int s[20],n[20];int main () { cin>>s;for (int i=0;i<s.length (); i++) s[i+1]= (int) (s[i]-' a ' + 1); for (int i=1;i<=s.length () +1;i++) for (int k=1;k<=26;k++) {n[i]=k;for (int j=1;j<=i-1;j++) n[j]=s[j];for (int j=i;j<=s.length (); j + +) N[j+1]=s[j] ; int l=1,r=s.length () +1;bool f=false;while (N[r]==n[l]) {R--, l++;if (l>=r) {f=true;break;}} if (f) {for (int j=1;j<=s.length () +1;j++) cout<< (char) (' a ' +n[j]-1); Cout<<endl;return 0;}} cout<< "NA" <<endl;return 0;}
B. Violence DFS each color
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include < Algorithm> #include <cstring>using namespace Std;int tot=0,n,m,h[105],q,de,c[105],v[105];struct edge{int c,y , NE;} E[10000];void Add (int x,int y,int c) {Tot++;e[tot].y=y;e[tot].c=c;e[tot].ne=h[x];h[x]=tot;} void Dfs (int x,int now) {if (x==de) {C[now]=1;return;} for (int i=h[x];i;i=e[i].ne) {if (E[i].c!=now) Continue;int y=e[i].y;if (!v[y]) {V[y]=1;dfs (y,now); v[y]=0;}}} int main () { scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) {int x,y,co;scanf ("%d%d%d", &x,&y, &CO); ADD (X,Y,CO); ADD (Y,X,CO);} scanf ("%d", &q), while (q--) {int x;scanf ("%d%d", &x,&de); memset (V,0,sizeof (v)); Memset (C,0,sizeof (c)); v[ x]=1;for (int i=h[x];i;i=e[i].ne) {int Y=e[i].y;v[y]=1;dfs (Y,E[I].C); v[y]=0;} int ans=0;for (int i=1;i<=m;i++) Ans+=c[i];cout<<ans<<endl;} return 0;}
C.
This problem is obviously a DP, but the violent DP timeout hyperspace.
Careful observation reveals that there are up to 500 walking lengths, such as D=1, which are assumed to increase by 1 each time, less than 250 per 30000, and one step at a time.
This time DP is 500*30000, can live.
Note that the problem says more than 30000 can not go!!
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include < algorithm> #include <cstring> #define M 30005#include <queue>using namespace std;struct data{int p,n;}; int C[m],n,d,f[m][505],b[m],k[m];int Main () {int ma=0; scanf ("%d%d", &n,&d), for (int i=1;i<=n;i++) {int x;scanf ("%d", &x); C[x]++;ma=max (ma,x);} if (d<=250) {for (int i=1;i<=d+250;i++) b[i]=i,k[i]=i;} Else{int now=0;for (int i=d-250;i<=min (30000,d+250); i++) B[++now]=i,k[i]=now;} memset (F,-1,sizeof (f)); F[d][k[d]]=c[d];int ans=f[d][k[d]];for (int i=d;i<=min (ma,30000-1); i++) for (int j=1;j< =500;j++) {if (f[i][j]==-1) continue;int m=b[j];for (int now=max (m-1,1); now<=m+1;now++) {int de=i+now;if (de> 30000) Continue;f[de][k[now]]=max (F[de][k[now]],f[i][j]+c[de]), Ans=max (Ans,f[de][k[now]]);}} Cout<<ans<<endl;return 0;}
Exam a got the top three.
The problem of D is clear:
Find each connected block with x dots in the block.
If there is no ring, then this block to build x-1 edge can be guaranteed to meet all, because there is no ring, it must be topological sort, then the topology after the order of the points connected to a chain, you can meet the requirements.
If there is a ring, it will be X-ray side.
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include < algorithm> #include <cstring> #include <queue>using namespace Std;int v[200005],h2[200005],in[200005], H[200005],tot=0,k,n,m;struct edge{int Y,ne;} e[400005];queue<int> q;void Add (int x,int y) {tot++;e[tot].y=y;e[tot].ne=h[x];h[x]=tot;} void Add2 (int x,int y) {tot++;e[tot].y=y;e[tot].ne=h2[x];h2[x]=tot;} void Dfs (int x) {if (!in[x]) Q.push (x); tot++;v[x]=1;for (int i=h2[x];i;i=e[i].ne) {int y=e[i].y;if (v[y]) Continue;dfs (y) ;}} int main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) {int x,y;scanf ("%d%d", &x,&y); ADD (x, y); ADD2 (x, y); ADD2 (y,x); in[y]++;} int ans=0;for (int i=1;i<=n;i++) if (!v[i]) {tot=0;k=0;while (!q.empty ()) Q.pop ();d FS (i); if (tot==1) Continue;int ok=0 ; while (!q.empty ()) {int X=q.front (); Q.pop (); ok++;if (Ok>tot) break;for (int i=h[x];i;i=e[i].ne) {in[e[i].y]--;if (! IN[E[I].Y]) Q.push (E[I].Y);}} if (Ok==tot) ans+= (tot-1); else Ans+=tot;}cout<<ans<<endl;return 0;}
"Codeforces #286 (Div 2)" ABCD puzzle